do_action()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
do_action( string $hook_name, mixed $arg )
Описание
Эта функция вызывает все функции, привязанные к хуку-действию $hook_name.
Можно создавать новые хуки-действия, просто вызвав эту функцию и указав имя нового хука в параметре $hook_name.
Хукам можно передавать дополнительные аргументы, во многом так же, как и в apply_filters().
Пример использования:
// Функция обратного вызова действия.
function example_callback( $arg1, $arg2 ) {
// (возможно) что-то сделать с аргументами.
}
add_action( 'example_action', 'example_callback', 10, 2 );
/*
* Запустить действия, вызвав функцию 'example_callback()',
* которая привязана к `example_action` выше.
*
* - 'example_action' — это хук-действие.
* - $arg1 и $arg2 — дополнительные аргументы, передаваемые в функцию обратного вызова.
do_action( 'example_action', $arg1, $arg2 );
Оригинал (английский)
This function invokes all functions attached to action hook $hook_name.
It is possible to create new action hooks by simply calling this function, specifying the name of the new hook using the $hook_name 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.
do_action( 'example_action', $arg1, $arg2 );
Параметры
$hook_name
string
обязательный
$arg
mixed
необязательный
Исходный код
wp-includes/plugin.php:487
function do_action( $hook_name, ...$arg ) {
global $wp_filter, $wp_actions, $wp_current_filter;
if ( ! isset( $wp_actions[ $hook_name ] ) ) {
$wp_actions[ $hook_name ] = 1;
} else {
++$wp_actions[ $hook_name ];
}
// Do 'all' actions first.
if ( isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
$all_args = func_get_args(); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
_wp_call_all_hook( $all_args );
}
if ( ! isset( $wp_filter[ $hook_name ] ) ) {
if ( isset( $wp_filter['all'] ) ) {
array_pop( $wp_current_filter );
}
return;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
}
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[ $hook_name ]->do_action( $arg );
array_pop( $wp_current_filter );
}
История изменений
| Версия | Описание |
|---|---|
| 5.3.0 | Formalized the existing and already documented ...$arg parameter by adding it to the function signature. |
| 1.2.0 | Introduced. |

