WordPress函数文档do_action()
执行一个动作钩子,调用所有回调方法 描述 Execute functions hooked on a spec…
执行一个动作钩子,调用所有回调方法
描述
Execute functions hooked on a specific action hook.
This function invokes all functions attached to action hook $tag. It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $tag parameter. You can pass extra arguments to the hooks, much like you can with apply_filters(). This function works similar to apply_filters() with the exception that nothing is returned and only the functions or methods are called.
You can hook a function to an action hook using add_action().
用法
<?php do_action( $tag, $arg ); ?>
Multiple Arguments: <?php do_action( $tag, $arg_a, $arg_b, $etc ); ?>
参数
$tag
(string) (必填) The name of the hook you wish to execute.
默认值: None
$arg
(mixed) (可选) The list of arguments to send to this hook.
默认值: Empty string
返回值
This function does not return a value.
注意
- 使用到: global $wp_filter – Stores all of the 过滤器s and 动作s.
- 使用到: global $wp_actions – Increments the amount of times the 动作 was triggered.
历史
添加于 版本: 1.2.0
源文件
do_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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Execute functions hooked on a specific action hook.
*
* This function invokes all functions attached to action hook `$tag`. It is
* possible to create new action hooks by simply calling this function,
* specifying the name of the new hook using the `$tag` parameter.
*
* You can pass extra arguments to the hooks, much like you can with
* {@see apply_filters()}.
*
* @since 1.2.0
*
* @global array $wp_filter Stores all of the filters
* @global array $wp_actions Increments the amount of times action was triggered.
* @global array $merged_filters Merges the filter hooks using this function.
* @global array $wp_current_filter Stores the list of current filters with the current one last
*
* @param string $tag The name of the action to be executed.
* @param mixed $arg Optional. Additional arguments which are passed on to the
* functions hooked to the action. Default empty.
*/
function do_action($tag, $arg = ”) {
global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;
if ( ! isset($wp_actions[$tag]) )
$wp_actions[$tag] = 1;
else
++$wp_actions[$tag];
// Do ‘all’ actions first
if ( isset($wp_filter[‘all’]) ) {
$wp_current_filter[] = $tag;
$all_args = func_get_args();
_wp_call_all_hook($all_args);
}
if ( !isset($wp_filter[$tag]) ) {
if ( isset($wp_filter[‘all’]) )
array_pop($wp_current_filter);
return;
}
if ( !isset($wp_filter[‘all’]) )
$wp_current_filter[] = $tag;
$args = array();
if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
$args[] =& $arg[0];
else
$args[] = $arg;
for ( $a = 2, $num = func_num_args(); $a < $num;=“” $a++=“” )=“” $args[]=“func_get_arg($a);” sort=“” if=“” (=“” !isset(=“” $merged_filters[=“” $tag=“” ]=“” )=“” )=“” {=“” ksort($wp_filter[$tag]);=“” $merged_filters[=“” $tag=“” ]=“true;” }=“” reset(=“” $wp_filter[=“” $tag=“” ]=“” );=“” do=“” {=“” foreach=“” (=“” (array)=“” current($wp_filter[$tag])=“” as=“” $the_=“” )=“” if=“” (=“” !is_null($the_[‘function’])=“” )=“” call_user_func_array($the_[‘function’],=“” array_slice($args,=“” 0,=“” (int)=“” $the_[‘accepted_args’]));=“” }=“” while=“” (=“” next($wp_filter[$tag])=“” !=“=” false=“” );=“” array_pop($wp_current_filter);=“” }=“”>
|
相关
Actions:
has_action(),
add_action(),
do_action(),
do_action_ref_array(),
did_action(),
remove_action(),
remove_all_actions()
- 原文:http://codex.wordpress.org/Function_Reference/do_action
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!