apply_filters ( string $tag, mixed $value )

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

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

Source file: wp-includes/plugin.php

View source


Parameters

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


Used by


Source

function apply_filters( $tag, $value ) {
	global $wp_filter, $merged_filters, $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;

	// Sort.
	if ( !isset( $merged_filters[ $tag ] ) ) {
		ksort($wp_filter[$tag]);
		$merged_filters[ $tag ] = true;
	}

	reset( $wp_filter[ $tag ] );

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

	do {
		foreach ( (array) current($wp_filter[$tag]) as $the_ )
			if ( !is_null($the_['function']) ){
				$args[1] = $value;
				$value = call_user_func_array($the_['function'], array_slice($args, 1, (int) $the_['accepted_args']));
			}

	} while ( next($wp_filter[$tag]) !== false );

	array_pop( $wp_current_filter );

	return $value;
}


Changelog

  • Since: WordPress 0.71

User Contributed Notes

  1. Echo after Filtering

    echo apply_filters( $tag, $value );
    

    Get Filtered

    $myvar = apply_filters( $tag, $value );
    

    Additional Filter Arguments

    $myvar = apply_filters( $tag, $value, $param, $otherparam );
    

    For example:

    $myvar = apply_filters( 'example_filter', 'filter me', 'arg1', 'arg2 ');
    

    With the_title filter

    $my_custom_title = apply_filters('the_title', '  My Custom Title (tm)  ');
    $my_custom_title will now contain 'My Custom Title ™', since the_title filter applies wptexturize() and trim(), among others.
    
  2. Uh huh.

    apply_filters ( string $tag, mixed $value );

    function apply_filters( $tag, $value )

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

    Right. Tip: this does not make sense, in case you missed it.

You must log in before being able to contribute a note.