apply_filters()WP 0.71

Call the functions added to a filter hook.

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)

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 );
1 time — 0.000001 sec (speed of light) | 50000 times — 0.02 sec (speed of light) | PHP 7.0.32, WP 5.0.3

No Hooks.

Return

Mixed. The filtered value after all hooked functions are applied to it.

Usage

apply_filters( $hook_name, $value, ...$args );
$hook_name(string) (required)
The name of the filter hook.
$value(mixed) (required)
The value to filter.
...$args(mixed) (required)
Additional parameters to pass to the callback functions.

Examples

5

#1 Usage examples

Obtaining a filtered value:
$value = apply_filters( 'filter_name', $value );

Add filter function (this code need to be executed before apply_filters() call):

add_filter( 'filter_name', 'filter_function' );

function filter_function( $value ){
	return $value;
}
Filter value and display it:
echo apply_filters( 'filter_name', $value );
Pass Additional Arguments for filter function:
$value = apply_filters( 'filter_name', $value, $arg1, $arg2, ... );

For example:

$value = apply_filters( 'filter_name', 'filter me', 'arg1', 'arg2 ');

Add filter function with additional arguments:

add_filter( 'filter_name', 'filter_function', 10, 3 );

function filter_function( $value, $arg1, $arg2 ){
	return $value;
}
0

#2 Example of outputting formatted content posts through a filter.

Alternative to the_content() function:

global $post;
echo apply_filters( 'the_content', $post->post_content );

Notes

  • Global. WP_Hook[]. $wp_filter Stores all of the filters and actions.
  • Global. Int[]. $wp_filters Stores the number of times each filter was triggered.
  • Global. String[]. $wp_current_filter Stores the list of current filters with the current one last.

Changelog

Since 0.71 Introduced.
Since 6.0.0 Formalized the existing and already documented ...$args parameter by adding it to the function signature.

apply_filters() code WP 6.6.2

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