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' );
}
本文原创,作者:萨龙龙,其版权均为4166am金沙信心之选所有。
如需转载,请注明出处:/wordpress-functions-activate-plugin.html



\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b\u3002



\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u5728\u5c1d\u8bd5\u5305\u542b\u63d2\u4ef6\u6587\u4ef6\u4e4b\u524d\u5c06\u91cd\u5b9a\u5411\u8bbe\u7f6e\u4e3a\u9519\u8bef\u3002\u5982\u679c\u63d2\u4ef6\u5931\u8d25\uff0c\u5219\u91cd\u5b9a\u5411\u4e0d\u4f1a\u88ab\u6210\u529f\u6d88\u606f\u8986\u76d6\u3002\u6b64\u5916\uff0c\u4e0d\u4f1a\u66f4\u65b0\u9009\u9879\uff0c\u4e5f\u4e0d\u4f1a\u5728\u63d2\u4ef6\u9519\u8bef\u65f6\u8c03\u7528\u6fc0\u6d3b\u94a9\u5b50\u3002



\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u2026@\u8428\u9f99\u7f51\u7edc&appkey=746468751&pic=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&searchPic=true","qq":"https:\/\/connect.qq.com\/widget\/shareqq\/index.html?url=https:\/\/salongweb.com\/wordpress-functions-activate-plugin.html&title=WordPress \u529f\u80fd\u51fd\u6570\u2014\u2014 activate_plugin(\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b)&pics=https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg&summary=\u63cf\u8ff0



\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b\u3002



\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u5728\u5c1d\u8bd5\u5305\u542b\u63d2\u4ef6\u6587\u4ef6\u4e4b\u524d\u5c06\u91cd\u5b9a\u5411\u8bbe\u7f6e\u4e3a\u9519\u8bef\u3002\u5982\u679c\u63d2\u4ef6\u5931\u8d25\uff0c\u5219\u91cd\u5b9a\u5411\u4e0d\u4f1a\u88ab\u6210\u529f\u6d88\u606f\u8986\u76d6\u3002\u6b64\u5916\uff0c\u4e0d\u4f1a\u66f4\u65b0\u9009\u9879\uff0c\u4e5f\u4e0d\u4f1a\u5728\u63d2\u4ef6\u9519\u8bef\u65f6\u8c03\u7528\u6fc0\u6d3b\u94a9\u5b50\u3002



\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u2026&site=\u8428\u9f99\u7f51\u7edc","zone":"https:\/\/sns.qzone.qq.com\/cgi-bin\/qzshare\/cgi_qzshare_onekey?url=https:\/\/salongweb.com\/wordpress-functions-activate-plugin.html&title=WordPress \u529f\u80fd\u51fd\u6570\u2014\u2014 activate_plugin(\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b)&desc=\u63cf\u8ff0



\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b\u3002



\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u5728\u5c1d\u8bd5\u5305\u542b\u63d2\u4ef6\u6587\u4ef6\u4e4b\u524d\u5c06\u91cd\u5b9a\u5411\u8bbe\u7f6e\u4e3a\u9519\u8bef\u3002\u5982\u679c\u63d2\u4ef6\u5931\u8d25\uff0c\u5219\u91cd\u5b9a\u5411\u4e0d\u4f1a\u88ab\u6210\u529f\u6d88\u606f\u8986\u76d6\u3002\u6b64\u5916\uff0c\u4e0d\u4f1a\u66f4\u65b0\u9009\u9879\uff0c\u4e5f\u4e0d\u4f1a\u5728\u63d2\u4ef6\u9519\u8bef\u65f6\u8c03\u7528\u6fc0\u6d3b\u94a9\u5b50\u3002



\u5e94\u8be5\u6ce8\u610f\u7684\u662f\uff0c\u4e0b\u9762\u7684\u4ee3\u7801\u2026&summary=&site=\u8428\u9f99\u7f51\u7edc"}>



\u5df2\u6fc0\u6d3b\u7684\u63d2\u4ef6\u4e0d\u4f1a\u518d\u6b21\u5c1d\u8bd5\u6fc0\u6d3b\u3002



\u5b83\u7684\u5de5\u4f5c\u65b9\u5f0f\u662f\u5728\u5c1d\u8bd5\u5305\u542b\u63d2\u4ef6\u6587\u4ef6\u4e4b\u524d\u5c06\u91cd\u5b9a\u5411\u8bbe\u7f6e\u4e3a\u9519\u8bef\u3002\u5982\u2026","thumb":"https:\/\/demo.salongweb.com\/mnews\/images\/default-thumb.jpg","date":{"day":"02","year":"2019\/04"},"desc":"WordPress\u4e3b\u9898|WordPress \u4f01\u4e1a\u4e3b\u9898|WordPress \u5546\u57ce\u4e3b\u9898|\u9ad8\u54c1\u8d28\u7f51\u7ad9\u5b9a\u5236","style":{"light":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-light.png","title":"#242F40","text":"#5B6B82","meta":"#9AA7B9","border":"#EFF5FF","bg":"#fff"},"dark":{"logo":"https:\/\/pic.salongweb.com\/2022\/12\/logo-dark.png","title":"#B1C1E8","text":"#b3bdd3","meta":"#565E75","border":"#1C2032","bg":"#171B2C"}}} :loading="posterLoading">