I would like to know if there is a way that at the time of submitting the registration the user programmatically add a second role, so he would have the default role and the second, is it possible?
Thank you.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You could use the register_new_user hook which fires after the registration is complete. e.g.
function add_role_to_new_user( $user_id ) {
$new_user = get_userdata( $user_id );
if ( $new_user ) {
$new_user->add_role( 'a_second_role' );
}
}
add_filter( 'register_new_user', 'add_role_to_new_user' );
This will – I think – fire for all new users, including new administrators added in the dashboard. If that’s not what you want then you can add a check e.g. in_array( 'subscriber', $new_user->roles, true ) first before adding the new role.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0