deactivate_plugins

Deactivate a single plugin or multiple plugins.

Description

deactivate_plugins( (string|array) $plugins, (constant) $silent = false, (null) $network_wide = null ); 

The deactivation hook is disabled by the plugin upgrader by using the $silent parameter.

Parameters (3)

0. $plugins (string|array)
Single plugin or list of plugins to deactivate.
1. $silent — Optional. (constant) => false
Prevent calling deactivation hooks. Default is false.
2. $network_wide — Optional. (null) => null
Whether to deactivate the plugin for all sites in the network. A value of null (the default) will deactivate plugins for both the site and the network.

Usage

  1. if ( !function_exists( 'deactivate_plugins' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/plugin.php'; 
  3.  
  4. // Single plugin or list of plugins to deactivate. 
  5. $plugins = null; 
  6.  
  7. // Prevent calling deactivation hooks. Default is false. 
  8. $silent = false; 
  9.  
  10. // Whether to deactivate the plugin for all sites in the network. 
  11. // A value of null (the default) will deactivate plugins for both the site and the network. 
  12. $network_wide = null; 
  13.  
  14. // NOTICE! Understand what this does before running. 
  15. $result = deactivate_plugins($plugins, $silent, $network_wide); 
  16.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/plugin.php  
  1. function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) { 
  2. if ( is_multisite() ) 
  3. $network_current = get_site_option( 'active_sitewide_plugins', array() ); 
  4. $current = get_option( 'active_plugins', array() ); 
  5. $do_blog = $do_network = false; 
  6.  
  7. foreach ( (array) $plugins as $plugin ) { 
  8. $plugin = plugin_basename( trim( $plugin ) ); 
  9. if ( ! is_plugin_active($plugin) ) 
  10. continue; 
  11.  
  12. $network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin ); 
  13.  
  14. if ( ! $silent ) { 
  15. /** 
  16. * Fires before a plugin is deactivated. 
  17. * If a plugin is silently deactivated (such as during an update),  
  18. * this hook does not fire. 
  19. * @since 2.9.0 
  20. * @param string $plugin Plugin path to main plugin file with plugin data. 
  21. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 
  22. * or just the current site. Multisite only. Default is false. 
  23. */ 
  24. do_action( 'deactivate_plugin', $plugin, $network_deactivating ); 
  25.  
  26. if ( false !== $network_wide ) { 
  27. if ( is_plugin_active_for_network( $plugin ) ) { 
  28. $do_network = true; 
  29. unset( $network_current[ $plugin ] ); 
  30. } elseif ( $network_wide ) { 
  31. continue; 
  32.  
  33. if ( true !== $network_wide ) { 
  34. $key = array_search( $plugin, $current ); 
  35. if ( false !== $key ) { 
  36. $do_blog = true; 
  37. unset( $current[ $key ] ); 
  38.  
  39. if ( ! $silent ) { 
  40. /** 
  41. * Fires as a specific plugin is being deactivated. 
  42. * This hook is the "deactivation" hook used internally by register_deactivation_hook(). 
  43. * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename. 
  44. * If a plugin is silently deactivated (such as during an update), this hook does not fire. 
  45. * @since 2.0.0 
  46. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 
  47. * or just the current site. Multisite only. Default is false. 
  48. */ 
  49. do_action( "deactivate_{$plugin}", $network_deactivating ); 
  50.  
  51. /** 
  52. * Fires after a plugin is deactivated. 
  53. * If a plugin is silently deactivated (such as during an update),  
  54. * this hook does not fire. 
  55. * @since 2.9.0 
  56. * @param string $plugin Plugin basename. 
  57. * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network 
  58. * or just the current site. Multisite only. Default false. 
  59. */ 
  60. do_action( 'deactivated_plugin', $plugin, $network_deactivating ); 
  61.  
  62. if ( $do_blog ) 
  63. update_option('active_plugins', $current); 
  64. if ( $do_network ) 
  65. update_site_option( 'active_sitewide_plugins', $network_current );