执行挂钩在特定操作挂钩上的函数。
原型
do_action( string $tag, $arg = '' )
描述
此函数调用附加到action hook $ tag的所有函数。可以通过简单地调用此函数来创建新的动作挂钩,使用$ tag参数指定新挂钩的名称。
参数
$tag
(string)
(Required)
要执行的操作的名称。
$arg,...
(mixed)
(Optional)
传递给连接到操作的函数的其他参数。默认为空。
源文件
路径:wp-includes/plugin.php
<?php
...
function do_action($tag, $arg = '') {
global $wp_filter, $wp_actions, $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);
$wp_filter[ $tag ]->do_action( $args );
array_pop($wp_current_filter);
}
...
?>
其他
英文文档:https://developer.wordpress.org/reference/functions/do_action/