Redirect to custom url when registration fails?

The question:

When I want to redirect the user after successful registration, I just use “registration_redirect” filter hook. How about when registration fails, which filter hook should I use? I looked into wp-login.php but I didn’t see any filter/action hook that I could use. How do I go about this?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

You could add an htaccess redirect or try out the theme my login plugin: It has options for custom redirection.

Method 2

There is an more sophisticated solution instead of htaccess or plugin. For those interested in this topic, there’s my code to redirect after (exactly before) unsuccessfull resitration attempt.

Why before? Before an attempt for registration, WordPress checks for an username and email address, whether they are not empty, whether they are not already in DB, and whether email nor username are in correct. If so, it reports errors and you can hook to a function right after this check.

do_action( 'register_post', $sanitized_user_login, $user_email, $errors );

Next step – unhookable – is an attempt to create a new user. If this fails, that there is an another error message and you’ll be redirected to standard wp_login.php screen.

But, there is an but. This process, Next step, just calls wp_insert_user() and this function fails only on empty_user_login, existing_user_login and existing_user_email – otherwise it end successfully. And if you’ve read carefully, all these variables are checked before this function is called (this double check is logical as you can call wp_insert_user directly from your plugin).

So if register process fails after mentioned hook, it fails because there’s something wrong with your WordPress installation and so, it is not a fail of your redirection function hooked to register process.

And finally some code for those, ho just want to copy and paste a code for redirection to custom page after login fails:

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 );

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment