编辑“WordPress:Function Reference/register activation hook

跳转至: 导航、​ 搜索
警告:您没有登录。如果您做出任意编辑,您的IP地址将会公开可见。如果您登录创建一个账户,您的编辑将归属于您的用户名,且将享受其他好处。

该编辑可以被撤销。 请检查下面的对比以核实您想要撤销的内容,然后发布下面的更改以完成撤销。

最后版本 您的文本
第1行: 第1行:
The function <strong>register_activation_hook</strong> (introduced in WordPress 2.0) registers a plugin function to be run [[WordPress:Managing_Plugins#Plugin_Installation|when the plugin is activated]].
函数<strong>register_activation_hook</strong>(在WordPress2.0时引进)注册了一个插件函数,[[WordPress:Managing_Plugins#Plugin_Installation|插件激活时]],函数就会运行。
函数<strong>register_activation_hook</strong>(在WordPress2.0时引进)注册了一个插件函数,[[WordPress:Managing_Plugins#Plugin_Installation|插件激活时]],函数就会运行。
This is easier than using the <tt>activate_<var>pluginname</var></tt> action.


这比使用<tt>activate_<var>pluginname</var></tt> action要简单。
这比使用<tt>activate_<var>pluginname</var></tt> action要简单。
== Usage and parameters ==


== 用法和参数 ==
== 用法和参数 ==
%%% <?php register_activation_hook($file, $function); ?> %%%


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


; <var>$file</var>
; <var>$file</var>
; <var>$file</var>
: (string) Path to the [[WordPress:Writing_a_Plugin#Plugin_Files|main plugin file]] inside the <tt>wp-content/plugins</tt> directory. A full path will work.


: (string) <tt>wp-content/plugins</tt>目录中[[WordPress:Writing_a_Plugin#Plugin_Files|主要的插件文件]]的路径。全称路径能够运行。
: (string) <tt>wp-content/plugins</tt>目录中[[WordPress:Writing_a_Plugin#Plugin_Files|主要的插件文件]]的路径。全称路径能够运行。


; <var>$function</var>
; <var>$function</var>
; <var>$function</var>
: (callback) The function to be run when the plugin is activated. Any of [http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback PHP's callback pseudo-types] will work.
: (callback)插件激活时,将要运行的函数。任何[http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback PHP's callback pseudo-types]都会运行。
: (callback)插件激活时,将要运行的函数。任何[http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback PHP's callback pseudo-types]都会运行。
== Examples ==


== 例子 ==
== 例子 ==


如果你在下面
If you have a function called <tt>myplugin_activate()</tt> in the [[WordPress:Writing_a_Plugin#Plugin_Files|main plugin file]] at either
* <tt>wp-content/plugins/<var>myplugin</var>.php</tt> 或者
* <tt>wp-content/plugins/<var>myplugin</var>.php</tt> or
* <tt>wp-content/plugins/<var>myplugin</var>/<var>myplugin</var>.php</tt>
use this code:
register_activation_hook( __FILE__, 'myplugin_activate' );
 
如果你在* <tt>wp-content/plugins/<var>myplugin</var>.php</tt> 或者
* <tt>wp-content/plugins/<var>myplugin</var>/<var>myplugin</var>.php</tt>中的[[WordPress:Writing_a_Plugin#Plugin_Files|主要插件文件]]中有个函数称作<tt>myplugin_activate()</tt>
* <tt>wp-content/plugins/<var>myplugin</var>/<var>myplugin</var>.php</tt>中的[[WordPress:Writing_a_Plugin#Plugin_Files|主要插件文件]]中有个函数称作<tt>myplugin_activate()</tt>
使用这个代码:
使用这个代码:
register_activation_hook( __FILE__, 'myplugin_activate' );
register_activation_hook( __FILE__, 'myplugin_activate' );
This will call the <tt>myplugin_activate()</tt> function on activation of the plugin. This is a more reliable method than using the <tt>activate_<var>pluginname</var></tt> action.


这样插件一激活,就会调用<tt>myplugin_activate()</tt>函数。这个方法比使用<tt>activate_<var>pluginname</var></tt> action更可靠。
这样插件一激活,就会调用<tt>myplugin_activate()</tt>函数。这个方法比使用<tt>activate_<var>pluginname</var></tt> action更可靠。


See also [[WordPress:Function_Reference/register_deactivation_hook|register_deactivation_hook]]
也看看[[WordPress:Function_Reference/register_deactivation_hook|register_deactivation_hook]]
也看看[[WordPress:Function_Reference/register_deactivation_hook|register_deactivation_hook]]
请注意,您对站长百科的所有贡献都可能被其他贡献者编辑,修改或删除。如果您不希望您的文字被任意修改和再散布,请不要提交。
您同时也要向我们保证您所提交的内容是您自己所作,或得自一个不受版权保护或相似自由的来源(参阅Wordpress-mediawiki:版权的细节)。 未经许可,请勿提交受版权保护的作品!
取消 编辑帮助(在新窗口中打开)