apply_filters()
Проверено на WordPress 6.9, обновлено Источник: WordPress Developer Resources.
Сигнатура
apply_filters( string $hook_name, mixed $value, mixed $args ): mixed
Описание
Эта функция вызывает все функции, привязанные к хуку-фильтру $hook_name.
Можно создавать новые хуки-фильтры, просто вызвав эту функцию и указав имя нового хука в параметре $hook_name.
Функция также позволяет передавать хукам несколько дополнительных аргументов.
Пример использования:
// Функция обратного вызова фильтра.
function example_callback( $string, $arg1, $arg2 ) {
// (возможно) изменить $string.
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 3 );
/*
* Применить фильтры, вызвав функцию 'example_callback()',
* которая привязана к `example_filter` выше.
*
* - 'example_filter' — это хук-фильтр.
* - 'filter me' — фильтруемое значение.
* - $arg1 и $arg2 — дополнительные аргументы, передаваемые в функцию обратного вызова.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
Оригинал (английский)
This function invokes all functions attached to filter hook $hook_name.
It is possible to create new filter hooks by simply calling this function, specifying the name of the new hook using the $hook_name parameter.
The function also allows for multiple additional arguments to be passed to hooks.
Example usage:
// The filter callback function.
function example_callback( $string, $arg1, $arg2 ) {
// (maybe) modify $string.
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 3 );
/*
* Apply the filters by calling the 'example_callback()' function
* that's hooked onto `example_filter` above.
*
* - 'example_filter' is the filter hook.
* - 'filter me' is the value being filtered.
* - $arg1 and $arg2 are the additional arguments passed to the callback.
$value = apply_filters( 'example_filter', 'filter me', $arg1, $arg2 );
Параметры
$hook_name
string
обязательный
$value
mixed
обязательный
$args
mixed
необязательный
Возвращаемое значение
mixed
Исходный код
wp-includes/plugin.php:173
function apply_filters( $hook_name, $value, ...$args ) {
global $wp_filter, $wp_filters, $wp_current_filter;
if ( ! isset( $wp_filters[ $hook_name ] ) ) {
$wp_filters[ $hook_name ] = 1;
} else {
++$wp_filters[ $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 $value;
}
if ( ! isset( $wp_filter['all'] ) ) {
$wp_current_filter[] = $hook_name;
}
// Pass the value to WP_Hook.
array_unshift( $args, $value );
$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );
array_pop( $wp_current_filter );
return $filtered;
}
История изменений
| Версия | Описание |
|---|---|
| 6.0.0 | Formalized the existing and already documented ...$args parameter by adding it to the function signature. |
| 0.71 | Introduced. |

