Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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 '11 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 '11 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 '11 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 '11 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 '11 at 0:09

2 Answers 2

up vote 12 down vote accepted

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_plugin(), 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.

share|improve this answer
    
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 '11 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 '11 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 '11 at 14:35

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.

share|improve this answer
    
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 –  willow Sep 11 at 4:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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