函数参考/add action
wordpress.org.cn
includeonlydiv style=clear:both; background-color:#FAEBD7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This page is marked as incomplete. You can help Codex by expanding it./div/includeonlynoinclude
目录
Description
This Template is used by Codex:Template Messages.
Usage
pre 检查到模板循环:模板:Message /pre
Result
检查到模板循环:模板:Message
/noinclude
说明
在指定的action上挂载(挂钩机制)一个函数。
对于这些动作,您可以在Plugin API/Action Reference上查看。当在Wordpress内部调用do_action()时,这些动作将被触发。
用法
%%% <?php add_action( $tag, $function_to_add, $priority, $accepted_args ); ?> %%%
参数
- tt$tag/tt
- (string) (参数必需) 动作的名字。 (对于这些动作,您可以在Plugin API/Action Reference上查看。)
- Default: 没有默认值noinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter
The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- string
- integer
- boolean
- mixed
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- required
- optional
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.
wordpress.org.cn /div /noinclude
- tt$function_to_add/tt
- (callback) (参数必需) 动作触发时,被调用函数的名字。 注释:在PHP文档中 'callback' 类型的语法解释是有效的。
- Default: 没有默认值noinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter
The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- string
- integer
- boolean
- mixed
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- required
- optional
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.
wordpress.org.cn /div /noinclude
- tt$priority/tt
- (int) (参数可选) 函数的优先级。改变这个值可以让您的函数在其他函数之前执行,或者是之后执行。默认值为10,例如把这个值设置为5将更早的被执行,设置为12将会晚一些执行。也就是小的值被优先执行。
- Default: 10noinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter
The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- string
- integer
- boolean
- mixed
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- required
- optional
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.
wordpress.org.cn /div /noinclude
- tt$accepted_args/tt
- (int) (参数可选) 函数参数的个数。在 WordPress 1.5.1+, 当匹配的 do_action() 或apply_filters()运行,钩子函数可以传入格外的参数。例如动作comment_id_not_found可以传入评论的ID。
- Default: 1noinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Notes
This template is for standardizing how parameters look in the Function Reference and in Template Tags. Here is an example of this template being called: prenowiki检查到模板循环:模板:Parameter/nowiki/pre 检查到模板循环:模板:Parameter
The usage of this template is below: prenowiki检查到模板循环:模板:Parameter/nowiki/pre Let's take a closer look at the parameters..
- name
- The name of the parameter.
- datatype
- The datatype that should be given for this parameter when called.
- string
- integer
- boolean
- mixed
- description
- A short description of the parameter.
- importance
- Set this parameter to optional if the parameter is optional. Otherwise, do not declare this parameter—it defaults to required.
- required
- optional
- default
- If this parameter is optional, ttdefault/tt is the value that will be used if the parameter is not declared.
wordpress.org.cn /div /noinclude
示例
Simple Hook
新文章发布时,通过邮件通知好友。
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');
获取 参数
The hooked function takes one argument from the action. Specifically, the 'echo_comment_id' function, takes the argument $comment_ID. It then echos the value of the received argument.
function echo_comment_id($comment_ID) { echo "I just received $comment_ID"; } add_action('comment_id_not_found','echo_comment_id', 10, 1);
Notes
To find out the number and name of arguments for an action, simply search the code base for the matching do_action() call. For example, if you are hooking into 'save_post', you would find it in post.php:
do_action('save_post', $post_ID, $post);
Your add_action call would look like:
add_action('save_post', 'my_save_post', 10, 2);
And your function would be:
function my_save_post($post_ID, $post){ //code here }
Change Log
Since: 1.2.0
Source File
add_action() is located in onlyincludecodewp-includes/plugin.php/code/onlyinclude
div class=template-description style=padding: 0 1.5em; border: 1px solid #eeeeee; background-color: #f9f9f9
Template Description
Link to the source code on http://core.trac.wordpress.org/browser/.
Parameters
- filename
- (option) path to codetag/code (version) or codetrunk/code. This option is only used for a new function.br /Default: codetrunk/code -- trunk is the latest bleeding edge development version of WordPress.
Usage
Link to the stable version: pre检查到模板循环:模板:Trac/pre
Link to trunk: pre检查到模板循环:模板:Trac/pre
/div
wordpress.org.cn.
相关
Actions: has_action, add_action, do_action, do_action_ref_array, did_action, remove_action, remove_all_actions
includeonlydiv style=clear:both; background-color:#F7F7F7; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;See also index of Function Reference and index of Template Tags./div/includeonlynoinclude
Description
This Template is used by Codex:Template Messages.
Usage
pre 检查到模板循环:模板:Message /pre
Result
检查到模板循环:模板:Message
/noinclude
includeonlydiv style=clear:both; background-color:#EEEEFF; border:1px solid #CCCCCC; color:#000000; padding:7px; margin:0.5em auto 0.5em auto; vertical-align:middle;This article is marked as in need of editing. You can help Codex by editing it./div/includeonlynoinclude
Description
This Template is used by Codex:Template Messages.
Usage
pre 检查到模板循环:模板:Message /pre
Result
检查到模板循环:模板:Message
/noinclude