php – function to assign user role based on a field from usermeta


I need to get the content of a field in user meta role_type, and replace the actual user role based on the user selection.

I use this function to replace the user role:

add_action( 'user_register', 'myplugin_registration_save', 10, 1 );

function myplugin_registration_save( $user_id ) 
{
    $user = new WP_User( $user_id );

    $user->remove_role( 'subscriber' ); // Optional, you don't have to remove this role if you want to keep subscriber as well

    $user->add_role( $role_type );

}

I don’t know how to get the usermeta value, and replace role_type