apply_filters( string $hook_name, mixed $value )

Calls the callback functions that have been added to a filter hook.

Description

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

Parameters

$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.

Return

(mixed) The filtered value after all hooked functions are applied to it.

Source

File: wp-includes/plugin.php

function apply_filters( $hook_name, $value ) {
	global $wp_filter, $wp_current_filter;

	$args = func_get_args();

	// Do 'all' actions first.
	if ( isset( $wp_filter['all'] ) ) {
		$wp_current_filter[] = $hook_name;
		_wp_call_all_hook( $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;
	}

	// Don't pass the tag name to WP_Hook.
	array_shift( $args );

	$filtered = $wp_filter[ $hook_name ]->apply_filters( $value, $args );

	array_pop( $wp_current_filter );

	return $filtered;
}

Changelog

Version Description
0.71 Introduced.

© 2003–2021 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/apply_filters