WordPress at a glance

do_action() WP 1.2.0

Creates an event (a hook for a php function). For the function to work during the action, it should be added to this action with add_action().

In addition to actions, WordPress also has filters (see apply_filters()). They work in the same way, the only difference is that the filters pass a value to the php function and this value needs to be filtered and returned.

The main difference between actions and filters is that the data passed by the action to the function is not intended to be returned, and so the filtered.

Is the basis for: wp_footer(), wp_head()
✈ 1 time = 0.00007s = very fast | 50000 times = 0.03s = speed of light

No Hooks.

Return

Null. Nothing.

Usage

do_action( $tag, $arg );
$tag(string) (required)
The name of the action to be executed.
$arg,...(mixed) (required)
Additional arguments which are passed on to the functions hooked to the action.
Default: ''

Examples

#1 Usage example

You can use this function in themes, plugins etc. when you need to inject into the code execution process. For example, we do an action (do_action) from the plugin, although this action was added in the theme.

Suppose our plugin contains such code:

<?php 
// Arguments that will be passed on to the functions hooked to the action.
$a = $defined; // Some dinamic variable
$b = 'Some string';

// Do the action (activate the hook)
do_action( 'my_hook', $a, $b );

And functions.php of the theme contains such code:

function do_my_hook( $a, $b ){
	// if variable $a is equal true,
	// then, for example, delete the post with ID 10
	if( $a === true )
		wp_delete_post(10);
	// A here just display the value of the second variable
	echo '<br />'.$b;
} 
// Register the hook
add_action( 'my_hook', 'do_my_hook', 10, 2 );

Notes

  • Global. Array. $wp_filter Stores all of the filters
  • Global. Array. $wp_actions Increments the amount of times action was triggered.
  • Global. Array. $wp_current_filter Stores the list of current filters with the current one last

Changelog

Since 1.2.0 Introduced.

Code of do action: wp-includes/plugin.php WP 5.2.4

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

Related Functions

From tag: Actions (hooks)

More from category: Hooks (actions, filters)

vladlu 100vlad.lu
Editors: kama 100
No comments
    Hello, !     Log In . Register