WordPress:Function Reference/register activation hook

来自站长百科
Fludlen讨论 | 贡献2008年7月21日 (一) 16:40的版本
跳转至: 导航、​ 搜索

The function register_activation_hook (introduced in WordPress 2.0) registers a plugin function to be run when the plugin is activated.

函数register_activation_hook(在WordPress2.0时引进)注册了一个插件函数,插件激活时,函数就会运行。

This is easier than using the activate_pluginname action.

这比使用activate_pluginname action要简单。

Usage and parameters

用法和参数

%%% <?php register_activation_hook($file, $function); ?> %%%

%%% <?php register_activation_hook($file, $function); ?> %%%

$file
$file
(string) Path to the main plugin file inside the wp-content/plugins directory. A full path will work.
(string) wp-content/plugins目录中主要的插件文件的路径。全称路径能够运行。
$function
$function
(callback) The function to be run when the plugin is activated. Any of PHP's callback pseudo-types will work.
(callback)插件激活时,将要运行的函数。任何PHP's callback pseudo-types都会运行。

Examples

例子

If you have a function called myplugin_activate() in the main plugin file at either

  • wp-content/plugins/myplugin.php or
  • wp-content/plugins/myplugin/myplugin.php

use this code:

register_activation_hook( __FILE__, 'myplugin_activate' );

如果你在* wp-content/plugins/myplugin.php 或者

  • wp-content/plugins/myplugin/myplugin.php中的主要插件文件中有个函数称作myplugin_activate()

使用这个代码: register_activation_hook( __FILE__, 'myplugin_activate' );

This will call the myplugin_activate() function on activation of the plugin. This is a more reliable method than using the activate_pluginname action.

这样插件一激活,就会调用myplugin_activate()函数。这个方法比使用activate_pluginname action更可靠。

See also register_deactivation_hook 也看看register_deactivation_hook