Google

Redirect after failed login attempt in WordPress

Written on:December 16, 2011
Comments
Add One

Here’s another piece of php code for redirecting to any page after a login error in wordpress.

add_filter('login_redirect', '_catch_login_error', 10, 3);

function _catch_login_error($redir1, $redir2, $wperr_user)
{
	if(!is_wp_error($wperr_user) || !$wperr_user->get_error_code()) return $redir1;

	switch($wperr_user->get_error_code())
	{
		case 'incorrect_password':
		case 'empty_password':
		case 'invalid_username':
		default:
			wp_redirect('/login-failed'); // modify this as you wish
	}

	return $redir1;
}

3 Comments add one

  1. sweta says:

    hi
    thanks for ur code.It worked for me,it is able to redirect after failed login attempts , but the error message it is giving is not correct. It should show like incoreect password/username etc… but it shows error: not found..can u tell how to fix this?

    • amado says:

      hi sweta. for that you’d have to modify the code, for redirectioo to special page, for example if you want to show 3 different error pages. you would create a page for the ‘incorrect passowrd’ message, ‘empty password’ and ‘invalid username’, and then redirect accordingly:

          switch($wperr_user->get_error_code())
          {
              case 'incorrect_password': wp_redirect('/my-incorrect-password-page'); break;
              case 'empty_password': wp_redirect('/my-empty-password-page'); break;
              case 'invalid_username': wp_redirect('/my-invalid-username-page'); break;
              default:
                  wp_redirect('/other-error-page');
          }
      
  2. matt says:

    Hi, first of all thanks for the code, it works like a charm :) I modified it a little so that user is sent to the same page after failed login attmpt. Is it possible to take care of situation when user left both login and password fields empty?

Leave a Comment

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>