Using WordPress ‘deactivate_plugins()’ PHP function

The deactivate_plugins() WordPress PHP function deactivates a single plugin or multiple plugins. This function can be used to disable a plugin silently, without calling deactivation hooks, or to deactivate the plugin across all sites in a network, useful in a multisite setup.

Usage

Here’s a basic usage of the function:

deactivate_plugins( 'plugin-directory/plugin-file.php' );

In this example, the function is deactivating the plugin located at ‘plugin-directory/plugin-file.php’.

Parameters

  • $plugins (string|array) – Required. This is the single plugin or list of plugins to deactivate. If multiple plugins are to be deactivated, they should be in an array format.
  • $silent (bool) – Optional. If set to true, it prevents the function from calling deactivation hooks. Default is false.
  • $network_wide (bool|null) – Optional. If set, the function will deactivate the plugin for all sites in the network. A value of null will deactivate plugins for both the network and the current site. This is applicable to multisite only. Default is null.

More information

See WordPress Developer Resources: deactivate_plugins()

Examples

Deactivating a Single Plugin

This example deactivates a single plugin by specifying the plugin’s file path.

deactivate_plugins( 'my-plugin/my-plugin.php' );

Deactivating Multiple Plugins

This example deactivates multiple plugins. The plugins are listed in an array.

deactivate_plugins( array('plugin-one/plugin-one.php', 'plugin-two/plugin-two.php') );

Silently Deactivating a Plugin

This example deactivates a plugin without calling its deactivation hooks.

deactivate_plugins( 'my-plugin/my-plugin.php', true );

Deactivating a Plugin Network Wide

In a multisite setup, this example deactivates a plugin across all sites in the network.

deactivate_plugins( 'my-plugin/my-plugin.php', false, true );

Deactivating a Plugin if PHP Version is not met

This example shows how you can deactivate your own plugin if the server’s PHP version does not meet your plugin’s requirements.

class My_Plugin {
  public function __construct() {
    register_activation_hook( __FILE__, array( $this, 'activate' ) );
  }

  public function activate() {
    if ( version_compare( PHP_VERSION, '7.4', '<' ) ) {
      deactivate_plugins( plugin_basename( __FILE__ ) );
      wp_die( __( 'This plugin requires PHP Version 7.4 or greater.', 'my-plugin' ) );
    }
  }
}

new My_Plugin();