Trouble understanding apply_filters()

Try to see the function with better names:

apply_filters(
    $filter_name,     // used for add_filter( $filter_name, 'callback' );
    $value_to_change, // the only variable whose value you can change
    $context_1,       // context
    $context_2        // more context
);

So when that function is called as:

// wp-login.php line 94
apply_filters( 'login_body_class', $classes, $action );

You can use …

add_filter( 'login_body_class', 'function_to_change_login_body_class', 10, 2 );

… and get two variables passed to that function. You return the first, the second provides just more context:

function function_to_change_login_body_class( $classes, $action )
{
    if ( 'login' === $action )
        $classes[] = 'foo';

    if ( 'postpass' === $action )
        $classes[] = 'bar';

    return $classes;
}

The additional variables are there to make your decisions easier, not to change those too.

Leave a Comment