I want to display login form and process the login on the same page, so that if there is any login error it should display the error messages on the same page.

I have added this function:

add_filter( 'authenticate', 'custom_authenticate_username_password', 30, 3);
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;

After adding the above function, how can I actually display the error message on the custom page template?


One way you could get the error message to your custom login page would be to use wp_login_failed hook. It could look something like this:

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

function my_login_fail( $username ) {
    //redirect to custom login page and append login error flag
    wp_redirect(home_url( '?page_id=4' ) . "&login_error" );  

Obviously you should modify wp_redirect depending on permalinks you are using.

Then in your custom page template you can check the login_error flag and display error message:

if (isset($_GET['login_error'])){
    //print error message or do something else
| improve this answer | |

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.