wp loged in methods doesnt working like signon ord set cookie
wp loged in methods doesnt working like signon ord set cookie
hi everyone im trying to fix a bug in a wordpress site problme is when i try to register new users it works but they didnt logged in or it takes some time to displays a user loged in
this is my func and last method i used
public function verifyCode() {
$phone = $this->_preparePhoneNumber();
$this->_verifyOtp( $phone );
$user = get_user_by( 'login', $phone );
$isRegistration = false;
$user_password = wp_generate_password();
if ( ! $user ) {
$userId = wp_create_user( $phone, $user_password, $phone . '@persiapartstore.com' );
$user = new WP_User( $userId );
$user->set_role( $_POST['isVendor'] ? 'seller' : 'customer' );
$isRegistration = true;
$credentials = array(
'user_login' => $phone,
'user_password' => $user_password,
'remember' => true,
);
$user = wp_signon( $credentials, false );
} else {
$userId = $user->ID;
wp_set_current_user( $userId );
wp_set_auth_cookie( $userId );
}
wp_send_json_success( [ ...$this->_getLoginResponse( $user ), 'isRegistration' => $isRegistration ] );
}
by the way my login is working when user is existed but when i create a new user it doesnt logged in thnaks for your helps
and wp cookie is set in my browser after register but when i try is_user_logged_in() it dosent work
You're using wp_set_current_user and wp_set_auth_cookie for existing users, but not for new users. These functions are crucial for setting the current user and there are many things that needs to fix, I have revised your code, please check
public function verifyCode() {
$phone = $this->_preparePhoneNumber();
$this->_verifyOtp( $phone );
$user = get_user_by( 'login', $phone );
$isRegistration = false;
if ( ! $user ) {
$user_password = wp_generate_password();
$userId = wp_create_user( $phone, $user_password, $phone . '@persiapartstore.com' );
$user = get_user_by( 'id', $userId ); // Reload user data
$user->set_role( $_POST['isVendor'] ? 'seller' : 'customer' );
$isRegistration = true;
wp_set_current_user( $userId );
wp_set_auth_cookie( $userId, true ); // Set the login cookie
} else {
$userId = $user->ID;
wp_set_current_user( $userId );
wp_set_auth_cookie( $userId );
}
wp_send_json_success( [ ...$this->_getLoginResponse( $user ), 'isRegistration' => $isRegistration ] );
}
January 10, 2024
Turbo Multi-language Translator
Make the better internet purchasing globaly
Turbosify SEO Speed Booster
5.0 (7)•Free plan available
Get better conversions by improving store loading speed
Installed
Turbo Multi-language Chat - AI Customer service in one hand
January 10, 2024