23

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
  • 2
    What about the existing documentation here and here does not explain it adequately for you?
    – t31os
    Jan 30, 2014 at 11:01
  • 5
    I think this is actually a good question. As a new wordpress dev this can be a confusing concept.
    – Shawn
    Sep 29, 2014 at 18:00

2 Answers 2

33

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.

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

The example above is the classic example that kills a newby developer.. explained in a rude - people-friendly way, you can use apply_filters to create a specific custom filter you can later on modify through add_filter. On the other side, if you use an already made hook, you can straight use add_filter.. The logic, is the tipical non-logic of development, so you need to look more examples and practice a bit in order to "understand" how they work.. I know that even my explanation could not sound totally clear, (and most of our coworkers could kill me for being incorrect) but it looks to me the most understandable explanation I have found on the Internet..

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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