1

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

In his comment He Shiming noted..

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;   
    }
}
8
  • Please don't "extend" the question. Referencing the other question is fine, but write a stand-alone question of your own. What exactly are you trying to accomplish?
    – s_ha_dum
    Mar 22 '14 at 15:21
  • Sorry, you're right! I am trying to translate the comment of Shiming into code but I don't know how this could be done.
    – SPi
    Mar 22 '14 at 15:42
  • Check if $_SERVER['REQUEST_URI'] is /wp-login.php?checkemail=registered, if yes then redirect to a page.
    – SPi
    Mar 22 '14 at 15:49
  • Where are you trying to redirect to?
    – s_ha_dum
    Mar 22 '14 at 15:57
  • To a custom page with a "Thanks for registering"-message, like mysite.com/succesful_registration
    – SPi
    Mar 22 '14 at 16:02
1

http://codex.wordpress.org/Plugin_API/Action_Reference/user_register

The question you referenced uses the 'register_post' hook which is just before the user gets entered into the database. If the registration is successful, that is when it sounds like you want to redirect. So, use the hook that fires immediately after the user is entered into the database.

TL;DR Use the 'user_register' hook instead of 'login_head'

1
  • This doesn't seem to work either. I edited my post and included the other functions that handle incomplete/invalid registrations. I feel like I have to edit them instead of adding another function for handling correct registrations..
    – SPi
    Mar 24 '14 at 9:33
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' );
    }
}

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.