^ 回到顶部
  • 人生没有定律,每个人都有自己的节奏
  • 本站wordpress建站教程均通过实践后发布,希望对你有帮助
  • 希望你的坚持,都是因为热爱,而不是因为不甘心
  • 5年wordpress建站经验,5星服务品质
  • 那些不愿意让你吃亏的人,才是真正值得你深交的人,也是值得你付出时间的人
  • 阿里云1核2g仅需102元/年,今日10点开抢

wordpress函数deactivate_plugins()用法示例

deactivate_plugins( string|array $plugins,  bool $silent = false,  mixed $network_wide = null )

Deactivate a single plugin or multiple plugins.


描述

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


参数

$plugins

(string|array)
(Required)
Single plugin or list of plugins to deactivate.

$silent

(bool)
(Optional)
Prevent calling deactivation hooks. Default is false.

Default value: false

$network_wide

(mixed)
(Optional)
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.

Default value: null


源代码

File: wp-admin/includes/plugin.php

function deactivate_plugins( $plugins, $silent = false, $network_wide = null ) {
	if ( is_multisite() )
		$network_current = get_site_option( 'active_sitewide_plugins', array() );
	$current = get_option( 'active_plugins', array() );
	$do_blog = $do_network = false;

	foreach ( (array) $plugins as $plugin ) {
		$plugin = plugin_basename( trim( $plugin ) );
		if ( ! is_plugin_active($plugin) )
			continue;

		$network_deactivating = false !== $network_wide && is_plugin_active_for_network( $plugin );

		if ( ! $silent ) {
			/**
			 * Fires before a plugin is deactivated.
			 *
			 * If a plugin is silently deactivated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin               Path to the main plugin file from plugins directory.
			 * @param bool   $network_deactivating Whether the plugin is deactivated for all sites in the network
			 *                                     or just the current site. Multisite only. Default is false.
			 */
			do_action( 'deactivate_plugin', $plugin, $network_deactivating );
		}

		if ( false !== $network_wide ) {
			if ( is_plugin_active_for_network( $plugin ) ) {
				$do_network = true;
				unset( $network_current[ $plugin ] );
			} elseif ( $network_wide ) {
				continue;
			}
		}

		if ( true !== $network_wide ) {
			$key = array_search( $plugin, $current );
			if ( false !== $key ) {
				$do_blog = true;
				unset( $current[ $key ] );
			}
		}

		if ( ! $silent ) {
			/**
			 * Fires as a specific plugin is being deactivated.
			 *
			 * This hook is the "deactivation" hook used internally by register_deactivation_hook().
			 * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
			 *
			 * If a plugin is silently deactivated (such as during an update), this hook does not fire.
			 *
			 * @since 2.0.0
			 *
			 * @param bool $network_deactivating Whether the plugin is deactivated for all sites in the network
			 *                                   or just the current site. Multisite only. Default is false.
			 */
			do_action( "deactivate_{$plugin}", $network_deactivating );

			/**
			 * Fires after a plugin is deactivated.
			 *
			 * If a plugin is silently deactivated (such as during an update),
			 * this hook does not fire.
			 *
			 * @since 2.9.0
			 *
			 * @param string $plugin               Path to the main plugin file from plugins directory.
			 * @param bool   $network_deactivating Whether the plugin is deactivated for all sites in the network.
			 *                                     or just the current site. Multisite only. Default false.
			 */
			do_action( 'deactivated_plugin', $plugin, $network_deactivating );
		}
	}

	if ( $do_blog )
		update_option('active_plugins', $current);
	if ( $do_network )
		update_site_option( 'active_sitewide_plugins', $network_current );
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

Uses

  • wp-admin/includes/plugin.php:
    is_plugin_active()
  • wp-admin/includes/plugin.php:
    is_plugin_active_for_network()
  • wp-admin/includes/plugin.php:
    deactivate_{$plugin}
  • wp-admin/includes/plugin.php:
    deactivated_plugin
  • wp-includes/load.php:
    is_multisite()
  • wp-includes/plugin.php:
    plugin_basename()
  • wp-includes/plugin.php:
    do_action()
  • wp-includes/option.php:
    update_site_option()
  • wp-includes/option.php:
    get_site_option()
  • wp-includes/option.php:
    update_option()
  • wp-includes/option.php:
    get_option()
  • Show 6 more uses
    Hide more uses


Used By

  • wp-admin/includes/class-plugin-upgrader.php:
    Plugin_Upgrader::deactivate_plugin_before_upgrade()
  • wp-admin/includes/plugin.php:
    validate_active_plugins()

User Contributed Notes

  1. Skip to note content

    You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note

    Contributed by Codex

    Example

    
    /**
     * Deactivate plugin example class.
     */
    class WPDocs_Deactivate_Plugin {
    
    	/**
    	 * Constructor.
    	 */
    	public function __construct() {
    		register_activation_hook( __FILE__, array( $this , 'activate' ) );
    	}
    
    	/**
    	 * Attempts to activate the plugin if at least PHP 5.4.
    	 */
    	public function activate() {
    		// Check PHP Version and deactivate & die if it doesn't meet minimum requirements.
    		if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
    			deactivate_plugins( plugin_basename( __FILE__ ) );
    			wp_die( __( 'This plugin requires PHP Version 5.4 or greater.  Sorry about that.', 'textdomain' ) );
    		}
    		
    		// Do activate Stuff now.
    	}
    }
    new WPDocs_Deactivate_Plugin();
    

让小雨知道,这篇文章帮到了你
扫码关注微信公众号zs40086(微搜片)随时随地微信看片,抢先福利电影等你来

热门推荐

如有疑问,请前往问答中心反馈!

反馈