Function and usage of deactivate_plugins() in wordpress


Answers ( 1 )


    The deactivate_plugins() function in WordPress is a powerful tool for managing plugins within your WordPress installation. Here's a detailed explanation of the function and its usage:

    Function: deactivate_plugins()

    • Purpose: This function is used to deactivate a single plugin or multiple plugins within a WordPress site.


    • When a plugin is deactivated using this function, it allows for a silent deactivation, meaning that the normal deactivation hooks that might be triggered when deactivating a plugin can be prevented. This is particularly useful in scenarios like automatic updates or bulk operations where you may not want the usual deactivation procedures to run.


    1. $plugins (string|string[]) [required]:

      • Type: String or an array of strings.
      • Description: This parameter specifies the plugin file or files to deactivate. It can be a single plugin file path (as a string) or an array of plugin file paths.
    2. $silent (bool) [optional]:

      • Type: Boolean.
      • Description: This parameter determines whether the deactivation hooks should be prevented from running.
      • Default Value: false.
      • If set to true, it will disable the execution of the deactivation hook typically triggered when a plugin is deactivated.
    3. $network_wide (bool|null) [optional]:

      • Type: Boolean or null.
      • Description: This parameter is used to specify whether the plugin should be deactivated network-wide on a WordPress multisite installation.
      • Default Value: null.
      • If null, the plugin will be deactivated for both the entire network and the current site in a multisite setup. If true, it will deactivate only network-wide. If false, it will only deactivate for the current site.

    Sample Usage

    // Deactivate a single plugin
    deactivate_plugins( 'plugin-folder/plugin-file.php' );
    // Deactivate multiple plugins
    deactivate_plugins( array( 'plugin1/plugin1-file.php', 'plugin2/plugin2-file.php' ) );
    // Deactivate a plugin silently (without triggering deactivation hooks)
    deactivate_plugins( 'plugin-folder/plugin-file.php', true );
    // Deactivate a plugin network-wide in a multisite setup
    deactivate_plugins( 'plugin-folder/plugin-file.php', false, true );
    // Deactivate a plugin for both network and the current site in a multisite
    deactivate_plugins( 'plugin-folder/plugin-file.php', false, null );

    Use Cases

    • Automated Updates: When updating plugins automatically, you might want to deactivate them silently to avoid running deactivation hooks each time.
    • Bulk Operations: Useful in scenarios where you need to deactivate multiple plugins at once, perhaps as part of a maintenance routine or site overhaul.
    • Multisite Management: Essential for managing plugins across a network of sites, providing flexibility to deactivate plugins either network-wide or for a specific site.

    This function is a crucial tool for WordPress developers and site administrators, offering a programmable way to manage plugin states efficiently.

Leave an answer