17

I have searched for a suitable explanation of the difference between add_filter() and apply_filters() in here but couldn't find one.

Can anyone tell me what information or logic to consider before using add_filter or apply_filters in one context.

That makes the use of one imperative and not the other ?

  • Is it correct that add_filter just adds a function to the queue of functions waiting to be executed on a variable and apply_filters executes the functions in order?

  • Is it also correct that apply_filters when called with an argument (the name of the function to be run) will execute that function before all the others (if they exist) in the queue?

  • 2
    What about the existing documentation here and here does not explain it adequately for you? – t31os Jan 30 '14 at 11:01
  • 3
    I think this is actually a good question. As a new wordpress dev this can be a confusing concept. – Shawn Sep 29 '14 at 18:00
26

Most of the following can be found in the Codex:


apply_filters

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.

$value = apply_filters( $tag, $value, $var_1, $var_2, ... );

In essence:
You use apply_filters to filter a given $value - with respect to the value itself as well as optionally provided variables $var_1 through $var_n.


add_filter

Hook a function to a specific filter action.

add_filter( $tag, $function_to_add, $priority, $accepted_args );

In essence:
You use add_filter to hook a custom function to the given filter action ($tag), which you might have generated by apply_filters before (or it was a built-in filter action or stems from a plugin/your theme).


So, here's a fictional example:

function print_initials( $name ) {

    if ( ! is_string( $name ) ) {
        return;
    }

    $fragments = explode( ' ', $name );

    /**
     * Filter wether to print initials in reverse order.
     *
     * @param bool $reverse Print initials in reverse order?
     */
    if ( apply_filters( 'reverse_initials', FALSE ) ) {
        $fragments = array_reverse( $fragments );
    }

    foreach ( $fragments as $f ) {
        echo substr( $f, 0, 1 );
    }
}

print_initials( 'Some Guy' ); // outputs: SG

add_filter( 'reverse_initials', '__return_true' );

print_initials( 'Some Guy' ); // outputs: GS

Now, if we just call our function as is, the initials are printed from left to right—because this is what we defined as default behavior.

The second time, we get the initials in reverse order—because the filter function __return_true, which is hooked to our filter action, always returns TRUE and thus makes the initials be output from right to left.

  • 3
    This is a great answer and shows how you can override filters. Thanks. – Shawn Sep 29 '14 at 18:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.