Wordpress - Disable plugin / plugin action via theme

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.

  • 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.