WordPress函数文档add_action()
给一个动作钩子添加一个回调方法 描述 译文 将函数连接到指定action(动作)。 在Plugin API/A…
给一个动作钩子添加一个回调方法
描述
译文
将函数连接到指定action(动作)。
在Plugin API/Action Reference 上查看动作hook列表。wordpress核心调用do_action() 时触发动作。
原文
Hooks a function on to a specific action.
More specifically, this function will run the function $function_to_add when the event $hook occurs.
This function is an alias to add_filter().
See Plugin API/Action Reference for a list of action hooks. Actions are (usually) triggered when the WordPress core calls do_action().
用法
<?php add_action( $hook, $function_to_add, $priority, $accepted_args ); ?>
参数
$hook
(string) (必填) The name of the action to which $function_to_add is hooked. (See Plugin API/Action Reference for a list of action hooks). Can also be the name of an action inside a theme or plugin file, or the special tag “all”, in which case the function will be called for all hooks.
默认值: None
$function_to_add
(callback) (必填) The name of the function you wish to be hooked.
默认值: None
$priority
(int) (可选) Used to specify the order in which the functions associated with a particular action are executed. Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
默认值: 10
$accepted_args
(int) (可选) The number of arguments the hooked function accepts. In WordPress 1.5.1+, hooked functions can take extra arguments that are set when the matching do_action() or apply_filters() call is run. For example, the action comment_id_not_found will pass any functions that hook onto it the ID of the requested comment.
默认值: 1
返回值
(boolean)
Always True.
示例
Simple Hook
To email some friends whenever an entry is posted on your blog:
1
2
3
4
5
6
7
8
9
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function email_friends( $post_ID ) {
$friends = ‘[email protected], [email protected]’;
wp_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’ );
|
Accepted Arguments
A hooked function can optionally accept arguments from the action call, if any are set to be passed. In this simplistic example, the echo_comment_id function takes the $comment_id argument, which is automatically passed to when the do_action() call using the comment_id_not_found filter hook is run.
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function echo_comment_id( $comment_id ) {
echo ‘Comment ID ‘ . $comment_id . ‘ could not be found’;
}
add_action( ‘comment_id_not_found’, ‘echo_comment_id’, 10, 1 );
|
Using with a Class
To use add_action() when your plugin or theme is built using classes, you need to use the array callable syntax. You would pass the function to add_action() as an array, with your object as the first element, then the name of the class method, like so:
1
2
3
4
5
6
7
8
9
10
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
class My_Plugin_Class {
public function save_posts_hook() {
// do stuff here…
}
}
$my_class = new My_Plugin_Class;
add_action( ‘save_post’, array( $my_class, ‘save_posts_hook’ ) );
|
If the class method can be called statically, the approach is much like the way you would a function: you can just pass a string with the full method:
1
2
3
4
5
6
7
8
9
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
class My_Plugin_Class {
public static function save_posts_hook() {
// do stuff here…
}
}
add_action( ‘save_post’, ‘My_Plugin_Class::save_posts_hook’ );
|
注意
To find out the number and name of arguments for an 动作, 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:
<?php do_action( 'save_post', $post_ID, $post, $update ); ?>
Your add_action call would look like:
<?php add_action( 'save_post', 'my_save_post', 10, 3 ); ?>
And your function would be:
1
2
3
4
5
6
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function my_save_post( $post_ID, $post, $update ) {
// do stuff here
}
|
历史
添加于 版本 1.2.0
源文件
add_action() 函数的代码位于 wp-includes/plugin.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Hooks a function on to a specific action.
*
* Actions are the hooks that the WordPress core launches at specific points
* during execution, or when specific events occur. Plugins can specify that
* one or more of its PHP functions are executed at these points, using the
* Action API.
*
* @since 1.2.0
*
* @param string $tag The name of the action to which the $function_to_add is hooked.
* @param callback $function_to_add The name of the function you wish to be called.
* @param int $priority Optional. Used to specify the order in which the functions
* associated with a particular action are executed. Default 10.
* Lower numbers correspond with earlier execution,
* and functions with the same priority are executed
* in the order in which they were added to the action.
* @param int $accepted_args Optional. The number of arguments the function accepts. Default 1.
* @return true Will always return true.
*/
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
return add_filter($tag, $function_to_add, $priority, $accepted_args);
}
|
相关
Actions:
has_action(),
add_action(),
do_action(),
do_action_ref_array(),
did_action(),
remove_action(),
remove_all_actions()
- 原文:http://codex.wordpress.org/Function_Reference/add_action
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!