Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using [wp_login_form()][1] to display login form in a jQuery dialog window.

If user enters wrong password, the user is taken to the backend. I don't want that. Is there a way to notify user that he entered wrong password and still remain on the same page?

Before wp_login_form() came I was using a plugin. I'm kind of hoping I can avoid using a plugin for this.

My code:

wp_login_form(array(
  'label_remember' => __( 'Remember me' ),
  'label_log_in' => __( 'Login' )
));
share|improve this question

4 Answers

up vote 1 down vote accepted

wp_login_form() creates a form with an action attribute of site_url/wp-login.php and that means that when you click the submit button the the form is posted to site_url/wp-login.php which ignores redirect_to on errors (like wrong password) so in your case either go back to using a plugin or recreate the whole login process and that way you have control on errors, take a look at Check for correct username on custom login form which is very similar question.

share|improve this answer
ah yeah, good point. Thanks. – Steven Apr 26 '11 at 19:45

I came here from google. But the answer didn't satisfy me. I was looking for a while and found a better solution.

Add this to your functions.php:

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;
   }
}
share|improve this answer
Thanks Alexey, I will test this (as I'm still using a plugin) – Steven Jun 19 '12 at 23:29
3  
Alexey's solution works when wrong credentials are entered, but unfortunately fails when the user forgets to enter username or password. Apparently Wordpress doesn't even try to log the user in in this case, so wp_login_failed action is not performed. – Szczepan Hołyszewski Sep 12 '12 at 7:39

One addition to Alexey's answer. You can add a jquery function to check that one of the fields is not blank. That way the form will not submit unless there is something to check, preventing WordPress from redirecting to /wp-login.php.

                        <script>
                                $("#wp-submit").click(function() {
                                  var user = $("input#user_login").val();
                                    if (user == "") {
                                    $("input#user_login").focus();
                                    return false;
                                  }
                                 });
                          </script>   

Still not sure how to fix the forgot password aspect

share|improve this answer
2  
Please be aware that WordPress loads jQuery in "No Conflict" mode. The $ alias does not work. – s_ha_dum Feb 26 at 5:38
You also have to consider that user hits [enter] and not clicking t he login button. Also, you need to check for blank password as well. – Steven Feb 26 at 11:07

A solution for Szczepan Hołyszewski's point about empty fields in the accepted solution, the following jQuery will prevent going to the standard wp-login page: (add to login page template or footer.php)

jQuery("#loginform-custom").submit(function(){
     var isFormValid = true;
       jQuery("input").each(function()
       {
       if (jQuery.trim($(this).val()).length == 0){
       jQuery(this).addClass("submit_error");
       isFormValid = false;
       }     
     else {
     jQuery(this).removeClass("submit_error");
     }
     });
     return isFormValid;
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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