• sweta

    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

      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');
          }
       
      
  • matt

    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?

    • Dan

      function wp_authenticate($username, $password) {
      $username = sanitize_user($username);
      $password = trim($password);

      $user = apply_filters(‘authenticate’, null, $username, $password);

      if ( $user == null ) {
      $user = new WP_Error(‘authentication_failed’, __(‘ERROR: Invalid username or incorrect password.’));
      }

      $ignore_codes = array(’empty_username’, ’empty_password’);

      if (is_wp_error($user) && !in_array($user->get_error_code(), $ignore_codes) ) {

      // Put your code here

      }

      return $user;
      }

  • Henry

    @amado – thanks for this code. A really clean solution.

    @Matt – did you find out how to take care of the situation when users leave both fields blank? This would be the finishing touch making the solution complete.

  • Susie

    From someone just getting started with WordPress/php, having a great time. Where do you put this code?

    Thanks.

  • Thanks for this! Simple and effective

  • Wise

    Hi thx for your nice code, i have one question if u dont mind.
    What about if the both fields are empty? what should i do to ridirect them to login faild page?

    thanks alot.

  • Deniz

    Perfectly working, except “if both fields are empty”… will appreciate any help…
    Regards,

  • Whoelse

    Adding this code to functions.php will redirect if both the username and password fields are empty:


    add_action( 'wp_authenticate', '_catch_empty_user', 1, 2 );

    function _catch_empty_user( $username, $pwd ) {
    if ( empty( $username ) ) {
    wp_safe_redirect( $redirect_to_whereever_you_want );
    exit();
    }
    }

    I found it here:
    http://wordpress.stackexchange.com/questions/97560/redirect-after-empty-login-username-and-password

  • Sebastian

    Great! Saved me hours of time and rescued my day!

  • AWESOME!