WordPress: Function Reference/add action:修订间差异
来自站长百科
无编辑摘要 |
无编辑摘要 |
||
第1行: | 第1行: | ||
== 描述== | == 描述== | ||
Hooks函数到一个特别的[[WordPress:Plugin_API#Actions|action]]。请看看[[WordPress:Plugin_API#Current_Hooks_For_actions|Plugin API|插件API]]关于hooks for action的列表。 | Hooks函数到一个特别的[[WordPress:Plugin_API#Actions|action]]。请看看[[WordPress:Plugin_API#Current_Hooks_For_actions|Plugin API|插件API]]关于hooks for action的列表。 | ||
== 用法 == | == 用法 == | ||
%%% <?php add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%% | %%% <?php add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%% | ||
== 例子 == | == 例子 == | ||
给一些朋友发送电子邮件,询问你的博客上是否已经发表了一篇文章。 | 给一些朋友发送电子邮件,询问你的博客上是否已经发表了一篇文章。 | ||
<pre> | <pre> | ||
第32行: | 第18行: | ||
add_action('publish_post', 'email_friends'); | add_action('publish_post', 'email_friends'); | ||
</pre> | </pre> | ||
== 参数 == | == 参数 == | ||
; <tt>$tag</tt> : (''string'')你想要hook onto的action的名称。 | ; <tt>$tag</tt> : (''string'')你想要hook onto的action的名称。 | ||
; <tt>$function_to_add</tt> : (''callback'')你希望调用的函数的名称。注:[http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback the PHP documentation for the 'callback' type]中解释的任何语法都是有效的。 | ; <tt>$function_to_add</tt> : (''callback'')你希望调用的函数的名称。注:[http://us2.php.net/manual/en/language.pseudo-types.php#language.types.callback the PHP documentation for the 'callback' type]中解释的任何语法都是有效的。 | ||
; <tt>$priority</tt> :你的函数有多么重要。更改这个重要性,使得你的函数在其它函数之前或者之后得到调用。默认值是10,因此(例如)将这个值设置为5,会使得函数更早运行,如果设置为12,会使得函数迟一点运行。 | ; <tt>$priority</tt> :你的函数有多么重要。更改这个重要性,使得你的函数在其它函数之前或者之后得到调用。默认值是10,因此(例如)将这个值设置为5,会使得函数更早运行,如果设置为12,会使得函数迟一点运行。 | ||
; <tt>$accepted_args</tt> :你的函数拥有多少个参数。在WordPress1.5.1+,hooked 函数可以获得,matching do_action() 或者apply_filters() call运行时,设置的额外参数。例如,action <tt>comment_id_not_found</tt>会传递任何hook onto it需要的评论ID的函数。 | ; <tt>$accepted_args</tt> :你的函数拥有多少个参数。在WordPress1.5.1+,hooked 函数可以获得,matching do_action() 或者apply_filters() call运行时,设置的额外参数。例如,action <tt>comment_id_not_found</tt>会传递任何hook onto it需要的评论ID的函数。 |
2008年7月26日 (六) 15:57的最新版本
描述[ ]
Hooks函数到一个特别的action。请看看Plugin API|插件API关于hooks for action的列表。
用法[ ]
%%% <?php add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1); ?> %%%
例子[ ]
给一些朋友发送电子邮件,询问你的博客上是否已经发表了一篇文章。
function email_friends($post_ID) { $friends = 'bob@example.org, susie@example.org'; mail($friends, "sally's blog updated" , 'I just put something on my blog: http://blog.example.com'); return $post_ID; } add_action('publish_post', 'email_friends');
参数[ ]
- $tag
- (string)你想要hook onto的action的名称。
- $function_to_add
- (callback)你希望调用的函数的名称。注:the PHP documentation for the 'callback' type中解释的任何语法都是有效的。
- $priority
- 你的函数有多么重要。更改这个重要性,使得你的函数在其它函数之前或者之后得到调用。默认值是10,因此(例如)将这个值设置为5,会使得函数更早运行,如果设置为12,会使得函数迟一点运行。
- $accepted_args
- 你的函数拥有多少个参数。在WordPress1.5.1+,hooked 函数可以获得,matching do_action() 或者apply_filters() call运行时,设置的额外参数。例如,action comment_id_not_found会传递任何hook onto it需要的评论ID的函数。