admin管理员组

文章数量:1128510

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.

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 asked Mar 15, 2011 at 21:36 PNMGPNMG 1,3811 gold badge16 silver badges19 bronze badges 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 Commented 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 Commented 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 Commented 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 Commented 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 Commented Mar 16, 2011 at 0:09
 |  Show 4 more comments

4 Answers 4

Reset to default 35

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

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.

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

If you want disable runtime a plugin based on a condition without deactivate it persistently for all the clients (as deactivate_plugins()do) you should use the option_active_plugins filter in a plugin inside the mu-plugins folder as in the example below:

# file: wp-content/mu-plugins/disable-plugins.php

add_filter( 'option_active_plugins', function( $plugins ){
    if ( $my_condition ) {
        $plugins= array_diff($plugins, array('plugin_to_rm_1.php', 'plugin_to_rm_2.php'));    
    }
    return $plugins;
});

本文标签: Disable pluginplugin action via theme