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

Leave a Comment