Disabling a Plugin When a User Is Logged In

by

What if you could somehow disable plugins when they aren’t needed, but keep them when they are needed? This is something you don’t see very often, because the way WordPress handles plugin loading is not conducive to adding a bit of code to your child theme’s functions.php file.

This tutorial is focused on how to disable a plugin if a user is logged in. For example, we’ll talk about how to disable the Bloom plugin from Elegant Themes. Even though Bloom is one of the best tools ever for getting subscribers, sometimes you just don’t want the signup notification if a viewer is already logged in!

Pros & Cons

Why is this great?

  • Better UX (user experience)
  • Faster page loads (better SEO)
  • You’ll get to create a plugin (!)

Why is this a challenge?

  • Plugins are loaded even before user information is gathered
  • It requires FTP or cPanel access to create a new folder in your site
  • The code does not go in your child theme
  • It’s not as easy as simply “hiding” the content (i.e., { display: none; } )

Any time you prevent DOM elements from being created, you are increasing UX and SEO via reduced page load times.

Getting Started

We’ve scoured Stack Overflow, the WordPress Codex and other articles to figure out how to do this. Turns out it’s not as easy as we hoped, simply because of what was said before – plugins are loaded and active even before user information is generated.

We’ll keep this as simple as possible! It looks intimidating if you skim through, but if you carefully follow along you’ll find it’s much easier than you expected.

To start off with, you will need FTP access to your site or access to your host’s cPanel File Manager in order to create a subfolder within your site’s wp-content folder. There are some hosts (for example, WPEngine) that thankfullly already include this subfolder. But you will still need to be able to place a new file you create into that subfolder.

If you don’t have either FTP access or cPanel File Manager access to your site, then you will need to get one or the other before you continue.

Creating the Folder

Inside your site, you will need to create an mu-plugins folder within your wp-content folder. In the early days of WordPress, “MU” stood for “multi-user”.  Now it stands for “must use”.  Plugins within this folder are not accessible within the WP admin area – simply because they are, in fact, “must use”.

After you have created that folder, it should look something like this:

Creating the Plugin

OK. The next thing you need to do is create a new PHP file (stay with us!) in your favorite text editor. We will, in effect, create a new plugin. But one that is not accessible from your WordPress admin area. Paste the following code into that new file. As you read through it, hopefully the comments will help you understand what is going on. In the first comment block, fill in your own information. It’s not required, but it will give you good practice with the technique of creating a plugin.

<?php
/*
Plugin Name: Logged In Plugin Disabler
Plugin URI: http://www.your-url.com
Description:  Removes a plugin functionality when a user is logged in
Author: Your Name
Version: 1.0
Author URI: Your URL
*/

add_filter( 'option_active_plugins', 'disable_logged_in_plugin' );

function disable_logged_in_plugin( $plugins ) {
	
	// The 'option_active_plugins' hook occurs before any user information get generated,
	// so we need to require this file early to be able to check for logged in status
	require (ABSPATH . WPINC . '/pluggable.php');

    // If we are logged in, and NOT an admin...
    if ( is_user_logged_in() & !is_admin() ) {

        // Use the plugin folder and main file name here.
		//  Bloom is used here as an example
		$key = array_search( 'bloom/bloom.php' , $plugins );

        if ( false !== $key ) {
			// Remove the plugin reference, based on its key
            unset( $plugins[ $key ] );
        }
		
		// You can "deactive" other plugins here as well,
		//  using the code above as a template.
		
    }
    return $plugins;
}

Save the file as disable-logged-in-plugin.php. You will then need to put that file into the mu-plugins folder you created in the previous step.

Once you are done, take a look at the folder structure and make sure that your new file is located within the mu-plugins folder on your site.

We’re Done! And Final Thoughts

That’s it! You were hoping for more, yes? We hope so, because that means you are willing to learn.

Give this a try and let us know how it goes. As we mentioned earlier, you could use this technique to disable a plugin based on a variety of conditions. One that comes to mind is:

is_admin() instead of is_user_logged_in()

Leave us some comments on how this goes for you, or if you have any questions about this tutorial!

20 Comments
  1. Fabio Sarcona

    Excellent article. A possible alternative would be the deactivation of the plugin is not used on the current page.

    Example: if you use the plugin Contact Form 7 only on the contact page, it is useless that script and styles are also loaded in the other pages.

    An if / else who decides the plugin activation based on the visited page

    Fabio.

    Reply
    • Terry Hale

      That’s a very good point, Fabio. I was hoping to get people thinking about that kind of idea with this basic tutorial. Thanks for the feedback! 🙂

      Reply
      • Fabio Sarcona

        If you want in private I can give you some other idea for a plugin 😉

        Reply
  2. Sarah Oates

    Wow Terry, this is totally saved me today!! I had two clients wanting to move away from Bloom if I couldn’t fix this issue. Thanks for the tutorial, its fabulous for someone who doesn’t know php like me.

    Reply
    • Terry Hale

      So happy to hear that, Sarah! Glad it worked for you. 🙂

      Reply
  3. Spencer Pope

    Thats great Terry! Thank you for sharing this. Your tutorial was clear and easy to follow.

    Reply
    • Terry Hale

      Thanks for taking the time to leave a comment, Spencer. Anyone who writes a tutorial can tell you how much that is appreciated!

      Reply
  4. Tiit Sau

    Hmm … does not work for me on WP 4.7.2.

    Reply
    • Terry Hale

      Hi, Tiit. There has been nothing released in WordPress that should prevent this from working. It might be a conflict with another plugin. And you’ll want to make sure you put this plugin into the mu-plugins folder. 🙂

      Reply
      • Tiit Sau

        Hi Terry!

        Thank you for quick reaction. There was usual beginner mistake in code. I’m trying to make my admin interface faster switching off several plugins for logged in admin user. Look at my code – is this correct and shortest way to define additional keys?

        http://www.live.ee/my-disable-case.png

        Thanks!

        Reply
        • Terry Hale

          Hello again, Tiit!

          As far as I can see, your code looks great! Did that work for you?

          If not, let me know and we can investigate some more.

          Cheers!

          Reply
  5. Frank

    Hello! Really nice article.
    Do you think it can work the other way around too?

    For example, I need some plugins to not load (on my homepage ONLY though) for users who are NOT logged in.

    Please let me know.
    Thanks!

    Reply
    • Terry Hale

      Hi Frank, thanks for the comment!

      If you want a plugin to not load when on the home page, and a user is not logged in, then you could change line 20 of the code above to something like this:

      if ( is_front_page() & !is_user_logged_in() & !is_admin() ) {

      Reply
      • Frank

        Hey Terry, thanks for the reply.

        I cannot get it to work. Could you please give me the code for another plugin, let’s say Yoast SEO?

        Also, putting this in the MU folder causes a screen that says I need administrator access in the backend of my WordPress even though I’m already logged in as admin.

        Please let me know.
        Thanks!

        Reply
        • Terry Hale

          Hey Frank,

          In regards to Yoast SEO, your line (24, as above) would be:

          $key = array_search( ‘wordpress-seo/wp-seo.php’ , $plugins );

          As far as the rest, we’re getting outside the article’s generic scope, since I would need more info on what messages you are getting. The ‘mu-plugins’ folder might have permissions issues that you’d need to ask your host about, since it is not created by default unless you are running a multi-site.

          Hope this helps at least a little bit!

          Reply
          • Frank

            Unfortunately I cannot get it to work no matter what I tried.

            Are you aware of any plugin out there that does that (except Plugin Organizer)?

            Thanks again.

  6. Jay Patel

    Excellent…Bloom needs this and few other tweaks that Elegant Theme is taking its time to put in. Too bad.

    Question: Can you put this in function.php file in a child theme?

    Reply
  7. Amit

    Hello Terry, I never thought this way of disabling a plugin for logged in users. But yes, it’s a good idea to disable some plugins when it’s not needed for logged in Users. It can help the user to load the website faster and it will not distract them too.

    Reply
    • Terry Hale

      Thank you, Amit. 🙂

      Reply

Submit a Comment

Your email address will not be published. Required fields are marked *