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.

Возвращает

Null. Ничего.

Использование

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

Заметки

  • Global. Array. $wp_filter Stores all of the filters and actions.
  • 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.
Since 5.3.0 Formalized the existing and already documented ...$arg parameter by adding it to the function signature.

Код do_action() WP 5.5.1

<?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(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
		_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;
	}

	if ( empty( $arg ) ) {
		$arg[] = '';
	} elseif ( is_array( $arg[0] ) && 1 === count( $arg[0] ) && isset( $arg[0][0] ) && is_object( $arg[0][0] ) ) {
		// Backward compatibility for PHP4-style passing of `array( &$this )` as action `$arg`.
		$arg[0] = $arg[0][0];
	}

	$wp_filter[ $tag ]->do_action( $arg );

	array_pop( $wp_current_filter );
}

From tag: Actions (hooks)

More from category: Hooks (actions, filters)

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