A WordPress-centric search engine for devs and theme authors



do_action ›

Since1.2.0
Deprecatedn/a
do_action ( $tag, $arg = '' )
Parameters: (2)
  • (string) $tag The name of the action to be executed.
    Required: Yes
  • (mixed) $arg ...$arg Optional. Additional arguments which are passed on to the functions hooked to the action. Default empty.
    Required: No
    Default: (empty)
Defined at:
Codex:

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

Example usage:

// The action callback function
function example_callback( $arg1, $arg2 ) {
    // (maybe) do something with the args
}
add_action( 'example_action', 'example_callback', 10, 2 );

/
  Trigger the actions by calling the 'example_callback()' function that's
  hooked onto example_action above.
 
  - 'example_action' is the action hook
  - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = do_action( 'example_action', $arg1, $arg2 );


Source

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 );
}