26

I have installed the Google Analyticator plugin on my site. I am also using the MobilePress plugin to serve up a theme designed for non-smart-phone mobile browsers. I have set up Google Analytics for Mobile for use in this theme and would like to disable the Analyticator plugin since it includes a call to a larger js file.

Does anyone know if it is possible to disable a plugin using functions.php or some other method?

I believe I have located the code that adds the functions to the page, so if it's not possible to disable the entire plugin is it possible to stop the actions for executing? I have tried to disable them directly with no luck. Here is the code from the plugin file:

add_action('wp_head', 'add_google_analytics', 999999);
add_action('init', 'ga_outgoing_links');

I tried to remove those actions with:

remove_action('wp_head','add_google_analytics',9999999);
remove_action('init', 'ga_outgoing_links');

Any help is greatly appreciated.

9
  • As for me this should be working technically, so there is something about your timing of running this code that goes wrong (too early or too late). Please provide more information.
    – Rarst
    Mar 15, 2011 at 21:49
  • Yeah, I thought it would work as well. I don't have too much experience with add/remove actions so I wasn't sure how to debug it further. Even though EAMann's answer worked, what kind more information were you looking for?
    – PNMG
    Mar 15, 2011 at 22:29
  • I am slightly confused - isn't it easier to just disable plugin via normal means if it's need to be gone for good? Which EAMann's answer does only by code. I assumed you wanted it disabled dynamically on some condition...
    – Rarst
    Mar 15, 2011 at 22:33
  • I have a regular theme which is set in wordpress for any normal desktop browser. For this theme, I want to use the GA plugin. The mobilepress plugin uses php to get the user-agent string and serves up a different theme to either a smart-phone or a non-smart-phone. I wanted to use the functions.php file for the non-smart-phone theme to disable the GA plugin from putting the JS in the <head> for that theme only. It still needs to be enabled for the other two themes (desktop & smart-phone). Does that help to clarify?
    – PNMG
    Mar 15, 2011 at 23:42
  • Yes, that is what I thought. But deactivation will just disable plugin persistently and it will stay off until activated explicitly. I don't see it fitting your use case.
    – Rarst
    Mar 16, 2011 at 0:09

3 Answers 3

34

When WordPress activates a plugin, it calls the activate_plugin() function. This function activates the plugin in a sandbox and redirects somewhere else on success. It's been used by a few authors to programmatically activate plugin dependencies.

There's another function, deactivate_plugins(), that does a similar thing in reverse ... it's actually how WordPress deactivates plug-ins when you click on "deactivate" or "disable."

To deactivate an installed plugin, just call:

deactivate_plugins( '/plugin-folder/plugin-name.php' );

Or, to deactivate multiple plugins at once:

deactivate_plugins( array( '/first-plugin/first.php', '/second-plugin/second.php' ) );

There's a second parameter (the first is either a string or array of the plugins to disable) that allows you to disable the plugins without calling deactivation hooks. By default, it's set to false, and I recommend you leave it that way. Unless for some reason you want to bypass the deactivation, then you'd do:

deactivate_plugins( '/plugin-folder/plugin-name.php', true );

This would just turn off the plugin, but it wouldn't fire anything the plugin registered to do on deactivation. So if the plugin removes options or db tables when it's deactivated, you'd want to do this "silent" deactivation to preserve that information and use it elsewhere.

3
  • Great answer -- and not just compared to another, let's say, lackluster "here's a website" answer that might be somewhere nearby.
    – editor
    Mar 15, 2011 at 21:59
  • This code, placed in the functions.php will deactivate the plugin in the admin thereby disabling it from the other two themes. Thank you Rarst for point that out. I was wanting to just disable it for the one theme where the functions.php file was located.
    – PNMG
    Mar 16, 2011 at 1:19
  • You're only ever using one theme at a time on a given site ... but if you want to re-activate the plugin after deactivating the theme, that is also possible.
    – EAMann
    Mar 21, 2011 at 14:35
13

This is thanks to EAMann's brilliant answer above, and I thought it might be helpful to the original poster too...

I needed a solution to make sure users deactivated my plugin if they uploaded the premium version (to avoid potential conflicts). Previously I detected its state with is_plugin_active and showed an admin error message to prompt users to switch it off. This is MUCH smoother...

function deactivate_plugin_conditional() {
    if ( is_plugin_active('plugin-folder/plugin-name.php') ) {
    deactivate_plugins('plugin-folder/plugin-name.php');    
    }
}
add_action( 'admin_init', 'deactivate_plugin_conditional' );

Note: Didn't seem to work on register_activation_hook, but admin_init works like a charm.

1
  • 1
    Just to note i'm pretty sure is_plugin_active is already checked in the deactivate_plugins hook so it shouldn't need to do it again Sep 11, 2014 at 4:24
3

Here is my solution, create a plugin that can disable other plugins like so:

/*
 * Plugin Name: Disable/Configure Plugins
 * Description: Lets you disable/configure plugins based on environment variables
 * Author:      Chris Sewell
 */

/**
 * Disable specified plugins in development environment.
 * 
 * This is a "Must-Use" plugin. Code here is loaded automatically before regular plugins load.
 * This is the only place from which regular plugins can be disabled programatically.
 * 
 * Place this code in a file in WP_CONTENT_DIR/mu-plugins or specify a custom location 

 * to "development" or "production" in each particular server/environment.
 */

/* Disable specified plugins in development environment */
if (((getenv('environment') == 'production') || (getenv('environment') == 'alpha')) == false) {
    $plugins = array(
        'w3-total-cache/w3-total-cache.php'
    );
    require_once(ABSPATH . 'wp-admin/includes/plugin.php');
    deactivate_plugins($plugins);
}

Push this to production, enable it, then bring the database down to your local dev machine.

Source

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.