do_action()
do_action( string $tag, mixed $arg ) 执行挂接在特定动作挂钩上的函数。Ex…
do_action( string $tag, mixed $arg )
执行挂接在特定动作挂钩上的函数。
Execute functions hooked on a specific action hook.
目录锚点:#说明#参数#返回#源码#笔记
说明(Description)
do_action()函数 的作用是创建一个行为钩子。该函数让 wordpress 变得更加强大,扩展性更加好。我们不仅可以使用wordpress内建的钩子,还允许我们使用do_action()自定义钩子,您可以向钩子传递额外的参数,就像使用apply_filters()一样。
WordPress 中 do_action() 和 do_action_ref_array() 区别,其实就是 PHP 中 call_user_func 函数 和 call_user_func_array 函数的区别。
它们的第一个参数都是回调函数,do_action() 还可以有多个参数,它们都是回调函数的参数,do_action_ref_array() 只有两个参数,第二个参数是要被传入回调函数的数组。
如果传递一个数组给 do_action_ref_array(),数组的每个元素的值都会当做一个参数传递给回调函数,数组的 key 回调掉。
如果传递一个数组给 do_action(),整个数组会当做一个参数传递给回调函数,数字的 key 还会保留住。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $tag | (string) | 必需 | 要执行的操作的名称。 |
| $arg | (mixed) | 可选 | 传递给与操作挂钩的函数的其他参数。默认为空。 |
返回(Return)
无返回值
源码(Source)
/**
* 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);="" }="">
| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 5.3.0 | wp-includes/plugin.php:444 | 374 | 1 function |
笔记(Notes)
//先定义echo_meta,输出内容
function echo_meta(){
echo '<meta name="description" content="你xx" />';
//然后将echo_meta挂载到wp_meta上
add_action('wp_meta','echo_meta');
}
//然后我们去执行wp_meta
function wp_meta() {
do_action('wp_meta');
}
1. 我们先自定义了一个含有两个参数的回调函数;
2. 紧着接我们通过add_action()这个函数把这个回调函数挂载到了woshigouzi这个动作钩子上,设置动作执行的优先级为10,并让这个回调函数可以接受两个参数;
3. 我们为这两个参数赋值;
4. 通过do_action()函数来执行挂在到woshigouzi这个动作钩子上面的函数,并把两个参数传给挂载到woshigouzi上面的回调函数;这个回调函数就会把这两个参数输出了;
一句话理解do_action()函数就是:执行挂载到钩子上的动作(回调函数实现的)。
与do_action_ref_array区别举例
function test_callback(){
$args = func_get_args();
$num = func_num_args();
echo $num."个参数:";
echo "
<pre>";
print_r($args);
echo "</pre>
";
}
//然后我们分别使用 do_action() 函数 和 do_action_ref_array() 函数进行回调:
$args = array (
'foo' => 'bar',
'hello' => 'world',
0 => 123
);
do_action('test_callback', $args);
do_action_ref_array('test_callback', $args);
//最后输出结果:
//1 个参数:
Array
(
[0] => Array
(
[foo] => bar
[hello] => world
[0] => 123
)
)
//3个参数:
Array
(
[0] => bar
[1] => world
[2] => 123
)
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

还没有任何评论,赶紧来占个楼吧!