I am working on a multi-step registration process and I am having an issue right now. What I am trying to achieve is to have a first call sending the information needed for the user to register, so to create him and logging him in, and then to ask him to update the previously randomly generated password on a second step. Everything is done using AJAX and the Wordpress REST API so there is no page reload. What I observe is that my step number one is properly generating a user and logging him using this code:
$password = wp_generate_password();
$userdata = array(
'user_login' => $email,
'user_email' => $email,
'user_pass' => $password,
'display_name' => $firstname . ' ' . $name,
'first_name' => $firstname,
'last_name' => $name,
'role' => 'subscriber',
'user_registered' => (new \DateTime())->format('Y-m-d H:i:s')
);
$user_id = wp_insert_user( $userdata ) ;
// Autologin after registration
wp_set_current_user($user_id);
if (wp_validate_auth_cookie()==FALSE)
{
wp_set_auth_cookie($user_id, true, false);
}
But calling either wp_get_current_user
or global $current_user
during the additional step in order to update the generated password is retrieving only 0. Code:
$response = null;
$current_user = wp_get_current_user();
var_dump( $current_user );
Return:
object(WP_User)#3217 (7) {
["data"]=>
object(stdClass)#3218 (0) {
}
["ID"]=>
int(0)
["caps"]=>
array(0) {
}
["cap_key"]=>
NULL
["roles"]=>
array(0) {
}
["allcaps"]=>
array(0) {
}
["filter"]=>
NULL
}
Do you have any idea why ? Is that normal behaviour ? For information, I am using cookie authentication as the Javascript code calling my user API is embedded in my custom theme.
Thanks in advance,