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.
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:
Or, to deactivate multiple plugins at once:
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:
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.
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…
Note: Didn’t seem to work on register_activation_hook, but admin_init works like a charm.
Here is my solution, create a plugin that can disable other plugins like so:
Push this to production, enable it, then bring the database down to your local dev machine.
Source