WordPress 功能函数—— activate_plugin(已激活的插件不会再次尝试激活)

描述

已激活的插件不会再次尝试激活。

它的工作方式是在尝试包含插件文件之前将重定向设置为错误。如果插件失败,则重定向不会被成功消息覆盖。此外,不会更新选项,也不会在插件错误时调用激活钩子。

应该注意的是,下面的代码实际上决不会阻止文件中的错误。代码不应该在别处用于复制使用重定向工作的“sandbox”。

如果发现任何错误或输出文本,那么它将被用来确保成功重定向将更新错误重定向。

用法

activate_plugin( string $plugin, string $redirect = '', bool $network_wide = false, bool $silent = false )

参数

$plugin

(string) (必需) 插件文件相对于plugins目录的路径。

$redirect

(string) (可选) 要重定向到的URL。

默认值: ''

$network_wide

(bool) (可选) 是否为网络中的所有站点或仅当前站点启用插件。仅限多站点。

默认值:false

$silent

(bool) (可选) 是否阻止调用激活钩子。

默认值:false

返回

(WP_Error|null)在无效的文件上WP_Error或者成功时为null。

来源

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

function activate_plugin( $plugin, $redirect = '', $network_wide = false, $silent = false ) {
    $plugin = plugin_basename( trim( $plugin ) );
 
    if ( is_multisite() && ( $network_wide || is_network_only_plugin( $plugin ) ) ) {
        $network_wide        = true;
        $current             = get_site_option( 'active_sitewide_plugins', array() );
        $_GET['networkwide'] = 1; // Back compat for plugins looking for this value.
    } else {
        $current = get_option( 'active_plugins', array() );
    }
 
    $valid = validate_plugin( $plugin );
    if ( is_wp_error( $valid ) ) {
        return $valid;
    }
 
    if ( ( $network_wide && ! isset( $current[ $plugin ] ) ) || ( ! $network_wide && ! in_array( $plugin, $current ) ) ) {
        if ( ! empty( $redirect ) ) {
            wp_redirect( add_query_arg( '_error_nonce', wp_create_nonce( 'plugin-activation-error_' . $plugin ), $redirect ) ); // we'll override this later if the plugin can be included without fatal error
        }
        ob_start();
        wp_register_plugin_realpath( WP_PLUGIN_DIR . '/' . $plugin );
        $_wp_plugin_file = $plugin;
        include_once( WP_PLUGIN_DIR . '/' . $plugin );
        $plugin = $_wp_plugin_file; // Avoid stomping of the $plugin variable in a plugin.
 
        if ( ! $silent ) {
            /**
             * Fires before a plugin is activated.
             *
             * If a plugin is silently activated (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_wide Whether to enable the plugin for all sites in the network
             *                             or just the current site. Multisite only. Default is false.
             */
            do_action( 'activate_plugin', $plugin, $network_wide );
 
            /**
             * Fires as a specific plugin is being activated.
             *
             * This hook is the "activation" hook used internally by register_activation_hook().
             * The dynamic portion of the hook name, `$plugin`, refers to the plugin basename.
             *
             * If a plugin is silently activated (such as during an update), this hook does not fire.
             *
             * @since 2.0.0
             *
             * @param bool $network_wide Whether to enable the plugin for all sites in the network
             *                           or just the current site. Multisite only. Default is false.
             */
            do_action( "activate_{$plugin}", $network_wide );
        }
 
        if ( $network_wide ) {
            $current            = get_site_option( 'active_sitewide_plugins', array() );
            $current[ $plugin ] = time();
            update_site_option( 'active_sitewide_plugins', $current );
        } else {
            $current   = get_option( 'active_plugins', array() );
            $current[] = $plugin;
            sort( $current );
            update_option( 'active_plugins', $current );
        }
 
        if ( ! $silent ) {
            /**
             * Fires after a plugin has been activated.
             *
             * If a plugin is silently activated (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_wide Whether to enable the plugin for all sites in the network
             *                             or just the current site. Multisite only. Default is false.
             */
            do_action( 'activated_plugin', $plugin, $network_wide );
        }
 
        if ( ob_get_length() > 0 ) {
            $output = ob_get_clean();
            return new WP_Error( 'unexpected_output', __( 'The plugin generated unexpected output.' ), $output );
        }
        ob_end_clean();
    }
 
    return null;
}

用户贡献的笔记

基本例子

尝试激活插件,并在失败时返回WP_Error

$result = activate_plugin( 'plugin-dir/plugin-file.php' );
if ( is_wp_error( $result ) ) {
    // Process Error
}

插件缓存问题

在插件全部初始化之后,添加或修改插件文件时会导致插件缓存出现问题。这可以通过重新加载页面,将activate_plugin()作为单独的AJAX请求发送,或者如果需要,通过手动更新缓存来解决。以下示例;

$cache_plugins = wp_cache_get( 'plugins', 'plugins' );
if ( !empty( $cache_plugins ) ) {
    $new_plugin = array(
        'Name'        => $plugin_name,
        'PluginURI'   => $plugin_uri,
        'Version'     => $plugin_version,
        'Description' => $plugin_description,
        'Author'      => $author_name,
        'AuthorURI'   => $author_uri,
        'TextDomain'  => '',
        'DomainPath'  => '',
        'Network'     => '',
        'Title'       => $plugin_name,
        'AuthorName'  => $author_name,
    );
    $cache_plugins[''][$plugin_path] = $new_plugin;
    wp_cache_set( 'plugins', $cache_plugins, 'plugins' );
}
本文原创,作者:萨龙龙,其版权均为萨龙网络所有。
如需转载,请注明出处:https://salongweb.com/wordpress-functions-activate-plugin.html