X

Redirect failed login attempts to any location

Adding this snippet to the functions.php or your wordpress theme will allow you to redirect failed login attempts to any location.


add_action( 'wp_login_failed', 'my_front_end_login_fail' ); 

function my_front_end_login_fail( $username ) {
     $referrer = $_SERVER['HTTP_REFERER'];
     if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
          wp_redirect( $referrer . '?login=failed' ); 
          exit;
     }
}

Comments  Leave a Reply

  1. Thank you so much! I have been looking everywhere for something like this.

    I do have 2 questions, is it possible to add a redirect for when the user leaves the fields empty? and how can I return a incorrect login error message?

    thanks again!

  2. Thanks after so many snippet I tried and this one works. but now stuck with redirect on my page. I mean its redirecting to the last page but I want to notify my user that they have entered wrong userid or password. How to display that message? or may be redirect to my custom page.

    1. Redirect to a custom page would work best I think,

      1. Yeh I am fine with that but how to redirect? I am not master with php so don’t know where should I put my url for that page?

        1. Line 5 is the redirect,

          wp_redirect( $referrer . ‘?login=failed’ );

          http://codex.wordpress.org/Function_Reference/wp_redirect
          This page has more details on the function wp_reidrect.

      2. Alright! done. Thanks a lot for your instant help.

        One more thing why your last comment doesn’t have reply link?

        1. Glad to hear you got things working Great stuff. In regards to your comments question this is just the level of nested comments I allow. Since eventually it would nest so far we could barely read comments anymore  🙂

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our privacy policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

WordPress Launch Checklist

The Ultimate WordPress Launch Checklist

We've compiled all the essential checklist items for your next WordPress website launch into one handy ebook.
Yes, Send Me the Free eBook!