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

wordpress函数activate_plugin()用法示例

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

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


描述

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

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

需要注意的是,下面的代码决不会阻止文件中的错误。代码不应该在其他地方复制“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;
}

更新日志

Version 描述
2.5.0 Introduced.

相关函数

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,
		'描述' => $plugin_ 描述,
		'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' );
}

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

热门推荐

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

反馈