0

I've a code for auto-login after registration which is:

function auto_login_new_user( $user_id ) {
    wp_set_current_user($user_id);
    wp_set_auth_cookie($user_id);
    $request = $_SERVER["HTTP_REFERER"];
    wp_redirect($request);
    exit;
}
add_action( 'user_register', 'auto_login_new_user' );

what I need is to create a shortcode to control the time of redirect, I don't want it to redirect after $userID = wp_insert_user( $userDataArr );

I want the redirect to happen after adding some code for extra fields like so update_user_meta( $userID, $fieldsArr[$fieldN], $value);

I was thinking that I can do so by butting the ADD_ACTION code inside shortcode funtion

function my_login_shortcode(){
    add_action( 'user_register', 'auto_login_new_user' );
}
add_shortcode('mogincode','my_login_shortcode');

how can I do that ?

2
  • redirection has to happen before any content is sent to the browser, inside a shortcode is too late.
    – Milo
    Feb 4 '15 at 19:31
  • @Milo Thank you for that explanation, so How can I fix that problem ? Feb 5 '15 at 6:53
0

Because I was trying to make the redirect happen using shortcode in the registration and I didn't get the answer which can delay the redirect until I add custom textarea fields date to user profile page

  • before this step $userID = wp_insert_user( $userDataArr ) , I have no user ID yet, so I can't do update for unknown user id
  • after it the redirect happens before I reach update_user_meta( $userID, $fieldsArr[$fieldN], $value); code, so I'm loosing the date which coming from $_POST[$fieldN], even I have user id

what I've done was setcookie for the data coming form $_POST[$fieldN] before the redirect, like so:

setcookie($fieldN, $value, time()+3600*24,'/','.MYSITE.com');
$userID = wp_insert_user( $userDataArr );

and I'm checking if cookie exist at the top of page, and handle it (caching and when I finish I delete that cookie by setting its time to minus time -, php normal way)

if ( is_user_logged_in() ) {
    $userID = $current_user->ID;
    if(isset($_COOKIE[$fieldN])){   
        update_user_meta( $userID, $fieldN, $_COOKIE[$fieldN]);
        setcookie($fieldN, '', time()-3600*24,'/',$homecookieURL);
    }
}

now the visitors can register and in the same time adding extra date for custom profile textarea fields.

I fix my problem, so I decided to share this, may someone has better solution or may I help someone need similar one :)

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.