User role can’t be set
I don’t know, but it seems that the new WordPress version (6.0.2) overrides the role variable in the wp_insert_user
function. Before it worked properly, but now the value defined by default in the wordpress dashboard takes precedence over the wp_insert_user
function.
Here is the code that worked properly before. Depending on the page where the user was at the time of registration, his role is defined.
if (is_page('register')) {
$user_role="customer";
} elseif (is_page('become-agent')) {
$user_role="agent";
}
$user_infos = [
'user_login' => $user_login,
'user_pass' => $user_pass,
'user_email' => $user_email,
'user_registered' => date('Y-m-d H:i:s'),
'role' => $user_role
];
$new_user_id = wp_insert_user($user_infos);
But unfortunately there is only the default predefined role in the wordpress dashboard that is assigned. I need the role assigned to the registration.
But despite this, I tried to modify the role of the user just after registration with wp_insert_user, and the action hooks user_register
, add_user_role
and set_user_role
; None of them works
Leave an answer