Disable plugin / plugin action via theme

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.

3

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.

  • Some documentation from A HitchHacker’s Guide through WordPress

Leave a Comment