do_action( string $tag, mixed $arg )
执行挂接在特定动作挂钩上的函数。
Execute functions hooked on a specific action hook.
说明(Description)
do_action()函数 的作用是创建一个行为钩子。该函数让 wordpress 变得更加强大,扩展性更加好。我们不仅可以使用wordpress内建的钩子,还允许我们使用do_action()自定义钩子,您可以向钩子传递额外的参数,就像使用apply_filters()一样。
参数(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 )