deactivate_plugins()
Deactivates the specified plugins. You can specify deactivation parameters.
Often used by plugins themselves when a plugin has new features and proper installation requires reactivating the plugin.
Also can be useful when you need to automatically deactivate a plugin during activation, for example, if it does not support the current WP version or PHP version, etc.
It is recommended to call the function during the action admin_init.
Deactivation hooks (like deactivate_*) do not run during plugin update.
Use activate_plugins() when you need to activate a plugin.
Works only in the admin panel; if needed on the frontend, include the file:
require_once ABSPATH .'/wp-admin/includes/plugin.php';
Hooks from the function
Returns
null. Does not return anything.
Usage
deactivate_plugins( $plugins, $silent, $network_wide );
- $plugins(string/array) (required)
The plugin ID or an array of IDs.
The ID looks like the path to the plugin file relative to the plugins folder:
democracy/democracy.php.- $silent(boolean)
- Silent deactivation. true - means deactivate the plugin without firing deactivation actions (hooks deactivate_*).
Default: false (deactivation actions fire) - $network_wide(true/false/null)
Whether to deactivate the specified plugins for the network (when true also deactivates the plugin if it was network-activated).
null is equivalent to true. The value null (default) deactivates plugins for the current site and for the network.
Default: null
Examples
#1 Deactivate the specified plugin
add_action( 'admin_init', 'action_deactivate_plugins' );
function action_deactivate_plugins(){
deactivate_plugins( 'democracy-poll/democracy.php' );
} #2 Deactivate the plugin when activated if the PHP version is lower than 5.3
register_activation_hook( __FILE__, 'activate' );
function activate() {
// Check the version of PHP. Need 5.3+
if ( version_compare(PHP_VERSION, '5.3', '<') ) {
//deactivate_plugins( plugin_basename( __FILE__ ); // this is optional
// terminate the message output.
wp_die( 'The plugin requires PHP 5.3 or higher.' );
}
// Activate...
}
Changelog
| Since 2.5.0 | Introduced. |