WordPress Snippets at WPcustoms

Redirect to custom url on failed login attempt

If a users login fails you can redirect to a custom page – i.e. an error page where you can show a registration form or give further tips why the login failed. Reset password etc.


/**
 * Snippet Name: Redirect to custom url on failed login attempt
 * Snippet URL: https://wpcustoms.net/snippets/redirect-custom-url-failed-login-attempt/
 */
  function wpc_front_end_login_fail( $username ) {
     $referrer = $_SERVER['HTTP_REFERER'];  // where did the post submission come from?
     // if there's a valid referrer, and it's not the default log-in screen
     if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
          wp_redirect( $referrer . '?login=failed' );  // let's append some information (login=failed) to the URL for the theme to use
          // you can also use wp_redirect('http://yoursite.com/failed');
          exit;
     }
}
add_action( 'wp_login_failed', 'wpc_front_end_login_fail' );