Redirect if $_Server[‘REQUEST_URI’] is /wp-login.php?checkemail=registered

This question extents the question Redirect to custom url when registration fails?:

In his comment He Shiming noted..

Read More

that to redirect the user on a successful registration, one has to add_action for login_head and check whether $_SERVER['REQUEST_URI'] is /wp-login.php?checkemail=registered, and redirect to the correct one. Supposably there’s a filter named registration_redirect, but it doesn’t work

I’m trying to translate this into code, here’s what I got:

function redirect_after_success() {

  if ($_SERVER['REQUEST_URI']=='/wp-login.php?checkemail=registered'){

     $redirect_url = get_bloginfo('url') . '/register';
     wp_redirect( $redirect_url );
  exit;
}
}
add_action('login_head','redirect_after_success');

This function doesn’t seem to be correct. I still get redirected to http://../register/?login=failed even after successful registrations.

Here are the other functions that handle incomplete/invalid registrations:

 // 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;
    }
}

//hook empty login submit
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; 
    }   
}


// unsuccessfull registration
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') . '/register';
        //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;   
    }
}

Related posts

Leave a Reply

2 comments

  1. Did you try using template_redirect action? I always use it when there’s no other action that I can plug into.

    add_action( 'template_redirect', 'wpse138854_register_redirect' );
    function wpse138854_register_redirect() {
        if ( isset( $_GET['checkemail'] ) && 'registered' == $_GET['checkmail'] ) {
            wp_redirect( get_bloginfo('url') . '/register' );
        }
    }