7

I have made a login form on the front-end of my site using wp_login_form(). However, when the user types in an incorrect username/password they are redirected to the wp-admin login form.

How can you instead display any error messages above the login form? Ideally, I don't want the user to ever have to see the Wordpress admin section.

I have tried using the redirect_invalid_login() hook but this doesn't seem to work?

| improve this question | | | | |
12

just add the following code to your function.php. it will redirect back user from where the login form is submitted.

add_action( 'wp_login_failed', 'my_front_end_login_fail' );  // hook failed login

function my_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
      exit;
   }
}
| improve this answer | | | | |
  • 1
    how can we get errors like invalid passowrd or invalid username etc...??? – vrajesh Jun 26 '15 at 6:29
  • 1
    The best practice is not to show specific error but instead something like "invalid credentials". Showing "invalid password" means that the user exist on this site so that is one less thing to guess (and by itself knowing that an email is registered on some website can be a privacy issue). If you show "invalid password" then it means some user on this site use this password which is not ideal. And to get the info that there was an error you can check if $_GET['login'] == 'failed'. – idFlood Nov 30 '18 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.