apply_filters() – Calls the callback functions that have been added to a filter hook.

You appear to be a bot. Output may be restricted

Description

Calls the callback functions that have been 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 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 );

Usage

$mixed = apply_filters( $hook_name, $value, $args );

Parameters

$hook_name
( string ) required – The name of the filter hook.
$value
( mixed ) required – The value to filter.
$args
( mixed ) optional

Returns

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

Source

File name: wordpress/wp-includes/plugin.php


Lines:

1 to 39 of 39
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;
}
 

 View on GitHub View on Trac