Support » Fixing WordPress » Getting Username/Password Error Messages To Show When Using wp_login_form

  • I am using wp_login_form() but I am getting no error messages, it just redirects back to the login page. I don’t want it to direct to wp-login.php but I need it to display the same errors as wp-login.php does.

    Thanks for the help!

Viewing 2 replies - 1 through 2 (of 2 total)
  • If you are using the ‘login_redirect’ action, it will redirect right back to your form when there is an error. If you remove that action, the errors will be handled on the wp-login.php page, otherwise it can be redirected wherever you like upon success using the ‘redirect’ parameter in wp_login_form.

    Here is a method to send a basic “failed” notification back to your custom login page:

    // On failure, notify the custom form through a redirect with a new query variable
    add_action( 'wp_login_failed', 'custom_login_failed'), 10, 2 );
    // When a user leaves a field blank, return that as an error that will fire wp_login_failed
    add_filter( 'authenticate', 'custom_authenticate_username_password'), 30, 3);
    
    function custom_login_failed( $username )
    {
        $referrer = wp_get_referer();
    
        if ( $referrer && ! strstr($referrer, 'wp-login') && ! strstr($referrer,'wp-admin') )
        {
            wp_redirect( add_query_arg('login', 'failed', $referrer) );
            exit;
        }
    }
    
    function custom_authenticate_username_password( $user, $username, $password )
    {
        if ( is_a($user, 'WP_User') ) { return $user; }
    
        if ( empty($username) || empty($password) )
        {
            $error = new WP_Error();
            $user  = new WP_Error('authentication_failed', __('<strong>ERROR</strong>: Invalid username or incorrect password.'));
    
            return $error;
        }
    }

    More here, along with full code that includes redirecting your wp-login.php in all scenarios:
    http://wordpress.stackexchange.com/a/105224/11966

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Getting Username/Password Error Messages To Show When Using wp_login_form’ is closed to new replies.