WordPress at a glance

apply_filters() WP 1.0

Call the functions added to a filter hook.

The callback functions attached to filter hook $tag are invoked by calling this function. This function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter.

The function allows for additional arguments to be added and passed to hooks.

// Our 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 we
 * "hooked" to 'example_filter' using the add_filter() function above.
 * - 'example_filter' is the filter hook $tag
 * - '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 );
  • Global. Array. $wp_filter Stores all of the filters.
  • Global. Array. $wp_current_filter Stores the list of current filters with the current one last.
✈ 1 time = 0.000016s = very fast | 50000 times = 0.04s = speed of light

No Hooks.

Return

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

Usage

apply_filters( $tag, $value );
$tag(string) (required)
The name of the filter hook.
$value(mixed) (required)
The value on which the filters hooked to $tag are applied on.
$var,...(mixed) (required)
Additional variables passed to the functions hooked to $tag.

Code of apply_filters: wp-includes/plugin.php VER 5.0.1

<?php
function apply_filters( $tag, $value ) {
	global $wp_filter, $wp_current_filter;

	$args = array();

	// Do 'all' actions first.
	if ( isset($wp_filter['all']) ) {
		$wp_current_filter[] = $tag;
		$args = func_get_args();
		_wp_call_all_hook($args);
	}

	if ( !isset($wp_filter[$tag]) ) {
		if ( isset($wp_filter['all']) )
			array_pop($wp_current_filter);
		return $value;
	}

	if ( !isset($wp_filter['all']) )
		$wp_current_filter[] = $tag;

	if ( empty($args) )
		$args = func_get_args();

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

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

	array_pop( $wp_current_filter );

	return $filtered;
}

Related Functions

From tag: Filters (hooks)

More from category: Hooks (actions, filters)

No comments
    Hello, !     Log In . Register