返回介绍

activate_plugin()

发布于 2017-07-19 10:24:48 字数 6969 浏览 1759 评论 0 收藏 0

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

试图激活插件在“沙盒”和重定向成功


description

一个已经激活的插件不会试图再次激活。

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

需要注意的是,下面的代码决不会阻止文件中的错误。代码不应该在其他地方复制“sandbox”,它使用重定向来工作。

如果发现任何错误或输出文本,则将捕获它以确保成功重定向将更新错误重定向。


参数

$plugin
(string)
(Required)
从插件目录到主插件文件的路径。
$redirect
(string)
(Optional)
重定向的URL。Default value: ''
$network_wide
(bool)
(Optional)
是否启用网络中所有站点或当前站点的插件。只在网站开启多站时支持。
Default value: false
$silent
(bool)
(Optional)
是否阻止调用激活钩子。
Default value: false

返回值

(WP_Error|null) 文件无效将返回一个 WP_Error,成功时返回值值为 NULL。


源代码

File: 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 main plugin file from 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 main plugin file from 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;
}

更新日志

Versiondescription
2.5.0Introduced.

相关函数

Uses

  • wp-admin/includes/plugin.php:validate_plugin()
  • wp-admin/includes/plugin.php:is_network_only_plugin()
  • wp-admin/includes/plugin.php:activate_{$plugin}
  • wp-admin/includes/plugin.php:activated_plugin
  • wp-includes/l10n.php:__()
  • wp-includes/pluggable.php:wp_create_nonce()
  • wp-includes/pluggable.php:wp_redirect()
  • wp-includes/load.php:is_multisite()
  • wp-includes/functions.php:add_query_arg()
  • wp-includes/plugin.php:plugin_basename()
  • wp-includes/plugin.php:wp_register_plugin_realpath()
  • 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()
  • wp-includes/load.php:is_wp_error()
  • wp-includes/class-wp-error.php:WP_Error::__construct()

Used By

  • wp-admin/includes/plugin.php:activate_plugins()

使用举例

基本应用

试图激活插件,并返回 WP_Error 失败

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

错误消息

由于以下原因,插件将无法激活以下通用响应,包括:解析标题信息、使用插件缓存(参见WordPress对象缓存)或权限错误。

The plugin does not have a valid header

插件缓存问题

用插件缓存问题造成插件文件添加或修改后的插件都被初始化。这可以通过刷新页面解决,发送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' );
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文