do_action()执行一个动作钩子,调用所有回调方法

目录

描述

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().

do_action() 描述

用法

 <?php do_action$tag$arg ); ?> 

Multiple Arguments: <?php do_action$tag$arg_a$arg_b$etc ); ?> 

do_action() 用法

参数

$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

do_action() 参数

返回值

This function does not return a value.

do_action() 返回值

注意

  • 使用到: global $wp_filter - Stores all of the 过滤器s and 动作s.
  • 使用到: global $wp_actions - Increments the amount of times the 动作 was triggered.

do_action() 注意

历史

添加于 版本: 1.2.0

do_action() 历史

源文件

do_action() 函数的代码位于 wp-includes/plugin.php.

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * 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);="" }="">

do_action() 源文件

相关