WordPress禁用某个插件:deactivate_plugins

该函数为全局函数,用来停止某个插件

deactivate_plugins

停用一个或者多个插件。

deactivate_plugins( string|string[] $plugins, bool $silent = false, bool|null $network_wide = null )

参数

参数类型描述
$pluginsstring|string[]需要停用的单个(多个)插件路径
$silentbool可选,防止调用插件停用挂钩
$network_widebool|null可选,在多站点中是否为所有站点停用

源码

文件:wp-admin/includes/plugin.php 753行

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    = false;
    $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 plugin file relative to the 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( '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, true );
            if ( false !== $key ) {
                $do_blog = true;
                unset( $current[ $key ] );
            }
        }
 
        if ( $do_blog && wp_is_recovery_mode() ) {
            list( $extension ) = explode( '/', $plugin );
            wp_paused_plugins()->delete( $extension );
        }
 
        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 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 plugin file relative to the 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 );
    }
}

函数用例

$vs=2.0;

if($vs != 3.0){
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    deactivate_plugins( plugin_basename( __FILE__ ) );//必须引入该文件
    wp_die("版本不符合要求");
}

为TA充电
共{{data.count}}人
人已赞赏
函数文档

WordPress使用$wpdb查询一行数据:get_row

2021-6-1 13:40:51

函数文档

WordPress用户登录函数:wp_signon

2021-10-18 10:24:24

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
今日签到
有新私信 私信列表
搜索