Set User Role at Registration
-
I am trying to set the user role at registration based upon which registration code is used to register. This is the code that verifies if the custom registration code field matches either one of two values and works just fine:
function um_custom_validate_username( $key, $array, $args) { if ( isset( $args[$key] ) && $args[$key] !== 'CODE1' && $args[$key] !== 'CODE2' ) { UM()->form()->add_error( $key, __( 'Please enter valid registration code.', 'ultimate-member' ) ); } } add_action( 'um_custom_field_validation_registration_code', 'um_custom_validate_username', 30, 3 );
However, I then need to assign the user role based upon the code that was used. It appears that two hooks are needed: um_registraton_user_role and set_role. I may be wrong about that as I haven’t been able to get this to work:
function um_set_user_role ($key, $array, $args, $role, $submitted) { if ( isset( $args[$key] ) && $args[$key] == 'CODE1' ) { $ultimatemember->user->set_role('user_role_1'); return $role; } elseif (isset( $args[$key] ) && $args[$key] == 'CODE2') { $ultimatemember->user->set_role('user_role_2'); return $role; } } add_action( 'um_set_user_role', 'um_registration_user_role', 10, 2 );
While it doesn’t break registration at all, it also doesn’t set the user role as desired.
Banging my head. Any help would be appreciated.
EDIT: Now that I have stepped away, I am thinking it needs to be a single function.
- The topic ‘Set User Role at Registration’ is closed to new replies.