TIL: How to use add_filter() in Wordpress

Using Wordpress' add_filter() function to override the login message on a custom login form built with Theme My Login.

I've set up the Theme My Login Wordpress plugin to display a nice login form for a careers portal we're building at work. The form by default displays a "Log In" message, but what I'd like to see is "Please log in to view your application status".

What Is apply_filters()

Under the hood, the plugin uses apply_filters(...) to allow customizing the login message.

return apply_filters('tml_action_template_message', $message, $action);

Using add_filter()

To modify it, one needs only call add_filter( $filter_name, $func_name, $priority, $arg_count ) where $filter_name is tml_action_template_message and $func_name should be the name of a function which will return the new string for the login message. While $priority was not important in my case, for me it was important to pass 2 for the $arg_count or else I wouldn't receive both arguments passed from the original apply_filters() call.

add_filter( 'tml_action_template_message', 'custom_dashboard_login_message', 10, 2);

My Filter Function

The function called needs only return a string. It could be the same string every time, or it could differ based on URL variables, time of day, various arguments, etc.

function custom_dashboard_login_message( $message, $action = '' ) {
    if ( $action === 'login' ) {
        $message = 'Please log in to view your application status.';
    }
    return $message;
}

This did the trick for me!

April 28, 2018

« Speeding Up CFDirectory in Coldfusion - Firefox Bug: Flexbox Elements Won't Wrap to Next Page in Print »