萨龙网络
登录
首页-WordPress文章-WordPress Functions-正文

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

萨龙龙萨龙龙
WordPress Functions
6年前
0
0
1.23W

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

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

应该注意的是,下面的代码实际上决不会阻止文件中的错误。代码不应该在别处用于复制使用重定向工作的“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
萨龙龙

萨龙龙

侠客
一个平平常常的人,热爱生活、旅行和摄影,骑行去过西藏,一直计划再去。14年从江西骑行来到大理,现栖居于洱海边,食人间烟火,过简单生活,做简约设计!
1342.11M346.27W3.40W
分享:
WordPress 功能函数——absint(将值转换为非负整数)
WordPress 功能函数——absint(将值转换为非负整数)上一篇
WordPress 功能函数—— activate_plugins(激活多个插件)下一篇
WordPress 功能函数—— activate_plugins(激活多个插件)
相关文章
总数:7

WordPress 功能函数—— add_comments_page(将子菜单页添加到评论主菜单上)

描述 此函数具有一个功能,该功能将用于确定菜单中是否包含页面。 连接到处理页面输出的函数也必须检查用户是否具有所需的…
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
1.14W
0

WordPress 功能函数——absint(将值转换为非负整数)

用法 absint(mixed$maybeint) 将值转换为非负整数。 参数 $maybeint (mixed)(必需)您希望…
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
1.02W
0

WordPress 功能函数—— add_clean_index(向指定的表添加索引)

描述 向指定的表添加索引。 用法 add_clean_index(string$table,st…
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
8.94K
0

WordPress 功能函数—— activate_plugins(激活多个插件)

描述 激活多个插件。 当WP_Error返回,但这并不意味着该插件的一个有错误。这意味着一个或多个插件文件路径无效。…
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
7.93K
0

WordPress 功能函数—— add_blog_option(为指定的博客ID添加新选项)

描述 您不需要序列化值。如果需要序列化值,则在将其插入数据库之前将其序列化。请记住,资源不能序列化或作为选项添加。 …
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
8.90K
0

WordPress 功能函数—— add_action(将函数挂接到特定的操作上)

描述 WordPress核心执行期间,在特定点执行时或特定事件发生时,Actions是钩子。插件可以指定使用ActionAPI在…
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
2.11W
0

WordPress 功能函数—— addslashes_gpc(添加斜杠以转义字符串)

描述 添加斜杠以转义字符串。 如果设置了magic_quotes_gpc,将首先删除斜杠。 用法 …
萨龙龙萨龙龙
WordPress Functions
6年前
0
0
7.95K
0
评论表单游客 您好,欢迎参与讨论。
请输入昵称
请输入邮箱
请输入网址
0 / 100
评论列表
总数:0
萨龙网络
暂无评论,第一个评论下?