WordPress函数文档do_action_ref_array()
执行一个动作钩子,并向所有注册到它的动作传递一个数组 描述 Execute functions hooked …
执行一个动作钩子,并向所有注册到它的动作传递一个数组
描述
Execute functions hooked on a specific action hook, specifying arguments in an array.
This function is identical to do_action, but the arguments passed to the functions hooked to $tag are supplied using an array.
用法
<?php do_action_ref_array( $tag, $args ); ?>
参数
$tag
(string) (必填) The name of the action to be executed.
默认值: None
$args
(array) (必填) The arguments supplied to the functions hooked to $tag
默认值: None
注意
- This function can be useful when your arguments are already in an array, and/or when there are many arguments to pass. Just make sure that your arguments are in the proper order!
- Before PHP 5.4, your callback is passed a reference pointer to the array. Your callback can use this pointer to access all the array elements. Adding an 动作 and declaring a call back that hooks the above example 动作 could look like this:
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
add_action(‘my_action’, ‘my_callback’);
function my_callback( $args ) {
//access values with $args[0], $args[1] etc.
}
|
Because the array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.
- Regardless of PHP version, you can specify the number of array elements when adding the 动作, and receive each element in a separate parameter in the callback function declaration like so:
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
add_action(‘my_action’, ‘my_callback’, 10, 4 );
function my_callback( $arg1, $arg2, $arg3, $arg4 ) {
//access values with $args1, $args2 etc.
}
|
This method copies the array elements into the parameter variables. Any changes to the parameter variables do not affect the original array.
- As of PHP 5.4, the array is no longer passed by reference despite the function’s name. You cannot even use the reference sign ‘&’ because call time pass by reference now throws an error. What you can do is pass the reference pointer as an array element. Doing so does require all callbacks added to the 动作 to expect a reference pointer. This is not something you will see in WordPress 动作s. This technique is provided for informational purposes only.
1
2
3
4
5
6
7
8
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
do_action_ref_array( ‘my_action’, array( &$args ));
add_action(‘my_action’, ‘my_callback’);
function my_callback( &$args ) {
//access values with $args[0], $args[1] etc.
}
|
Because the original array was passed by reference, any changes to the array elements are applied to the original array outside of the function’s scope.
历史
添加于 版本: 2.1.0
源文件
do_action_ref_array() 函数的代码位于 wp-includes/plugin.php
.
1
2
3
4
5
6
7
8
9
10
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Execute functions hooked on a specific action hook, specifying arguments in an array.
*
* @since 2.1.0
*
* @see do_action() This function is identical, but the arguments passed to the
* functions hooked to $tag< are=”” supplied=”” using=”” an=”” array.=”” *=”” @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=”” array=”” $args=”” the=”” arguments=”” supplied=”” to=”” the=”” functions=”” hooked=”” to=”” `$tag`.=”” */=“” function=“” do_action_ref_array($tag,=“” $args)=“” {=“” 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;” 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_ref_array
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!