Ultimate guide on redirect to custom page after login / register fails

It’s quite easy to setup a frontend registration or login form. You can find a really extensive ammount of tutorials on this topic. And usually everything works great – your users do not see typical WordPress login/register page and are redirected to frontend of your website. Till either registration or login fails. Than most of solutions fails too – users are not redirected and see typical WordPress login/register screen. What now? It’s quite hard to find a solution on the internet – that’s why I’ve prepared this ultimate howto.

Actually, to redirect after login fails is well documented – there is a special hook for that in WordPress. You can find a great tutorials on this.

So in breaf: here’s my code based on this solution: How to Redirect WordPress Failed Logins (modified as original code stackups login=fail attrs):

// hook failed login
add_action('wp_login_failed', 'my_frontend_login_fail'); 
 
function my_frontend_login_fail($username){
    // Get the reffering page, where did the post submission come from?
    $referrer = add_query_arg('login', false, $_SERVER['HTTP_REFERER']);
 
    // 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')){
        // let's append some information (login=failed) to the URL for the theme to use
        wp_redirect($referrer . '?login=failed'); 
    exit;
    }
}

Ok, this was easy, wasn’t it? But wait! What has happen when a user submit your custom login form (frontend login form) empty? He/She will land on wp-login.php again instead on your custom login form. To fix this behaviour, add this code also:

add_action( 'login_head', 'my_frontend_login_no_pass_no_username' );

function my_frontend_login_no_pass_no_username(){
    $referrer = add_query_arg('login', false, $_SERVER['HTTP_REFERER']);
    if ( (!isset($_REQUEST['user_login']) || ( isset( $_REQUEST['user_login'] ) && trim( $_REQUEST['user_login'] ) == '' ) ) || (!isset($_REQUEST['user_pass']) || ( isset( $_REQUEST['user_pass'] ) && trim( $_REQUEST['user_pass'] ) == '' ) ) ){
        wp_redirect( add_query_arg('login', 'failed', $referrer) ); 
        exit; 
    }   
}

And now, what if we would like to redirect our user (not registered yet) to a custom page after unsuccessful registration attempt?

Have to say, that I’v originally published this solution, with extensive comment, on WordPress Answers – vote up, if you find this solution helpful, thx.

Again in breaf, here’s the code:

add_action('register_post', 'binda_register_fail_redirect', 99, 3);

function binda_register_fail_redirect( $sanitized_user_login, $user_email, $errors ){
    //this line is copied from register_new_user function of wp-login.php
    $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );
    //this if check is copied from register_new_user function of wp-login.php
    if ( $errors->get_error_code() ){
        //setup your custom URL for redirection
        $redirect_url = get_bloginfo('url') . '/registrace';
        //add error codes to custom redirection URL one by one
        foreach ( $errors->errors as $e => $m ){
            $redirect_url = add_query_arg( $e, '1', $redirect_url );    
        }
        //add finally, redirect to your custom page with all errors in attributes
        wp_redirect( $redirect_url );
        exit;   
    }
}