register_activation_hook()

为插件设置激活挂钩。

register_activation_hook(string $file,callable $function)

说明(Description)

当插件被激活时,将调用activate_PLUGINNAME钩子。在这个钩子的名称中,plugin name被替换为插件的名称,包括可选的子目录。例如,当插件位于wp content/plugins/sampleplugin中时/示例.php,则此钩子的名称将变为“activateu sampleplugin”/示例.php’.

当插件只包含一个文件并且(默认情况下)位于wp content/plugins时/示例.php这个钩子的名字是’activate_示例.php’.


参数(Parameters)

参数类型必填说明
$file(string)必需插件的文件名,包括路径。
$function(callable)必需函数挂接到“激活插件”操作。

返回(Return)

无返回值


源码(Source)

/**
 * Set the activation hook for a plugin.
 *
 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
 * called. In the name of this hook, PLUGINNAME is replaced with the name
 * of the plugin, including the optional subdirectory. For example, when the
 * plugin is located in wp-content/plugins/sampleplugin/sample.php, then
 * the name of this hook will become 'activate_sampleplugin/sample.php'.
 *
 * When the plugin consists of only one file and is (as by default) located at
 * wp-content/plugins/sample.php the name of this hook will be
 * 'activate_sample.php'.
 *
 * @since 2.0.0
 *
 * @param string   $file     The filename of the plugin including the path.
 * @param callback $function The function hooked to the 'activate_PLUGIN' action.
 */
function register_activation_hook($file, $function) {
	$file = plugin_basename($file);
	add_action('activate_' . $file, $function);
}
更新版本源码位置使用被使用
2.0.0wp-includes/plugin.php:78402

笔记(Notes)

单子类模式
注意,register_activation_钩子不能从另一个钩子(例如“plugins_loaded”或“init”)中注册,因为这些钩子都是在加载或激活插件之前调用的。
这里有一条评论,如果给出的信息不正确,可能会让人感到困惑。代码不必在主文件中,除非您正在执行类似于单个文件插件的操作。需要知道的是,第一个参数是激发代码所需的名称,而不是代码所在的文件。

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索