• Resolved pgrafix

    (@pgrafix)


    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.

    • This topic was modified 4 years, 9 months ago by pgrafix.
    • This topic was modified 4 years, 9 months ago by pgrafix.
    • This topic was modified 4 years, 9 months ago by pgrafix.
Viewing 15 replies - 1 through 15 (of 16 total)
  • Thread Starter pgrafix

    (@pgrafix)

    After some sleep I realized that the second function couldn’t work because it has no idea what registration code was used since $key was not within its scope. So, revised first function to:

    function um_custom_validate_username( $key, $array, $args ) {
    	global $regcode;
    	$regcode = $args[$key];
    	if ( isset( $regcode ) && $regcode !== 'CODE1' && $regcode !== '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 );

    This works fine and tells me that the global $regcode should be available for further assessment. It’s the second check and assign function that still doesn’t function. After re-reading the documentation I realize that set_role cannot be used until the user actually exists, thus, it will not work here.

    Any help would be greatly appreciated.

    Thread Starter pgrafix

    (@pgrafix)

    Got it working. For anyone who is interested in having a registration code enabled form that assigns a user role based upon the code:

    function um_custom_validate_username( $key, $array, $args ) {
    	global $regcode;
    	$regcode = $args[$key];
    	if ( isset( $regcode ) && $regcode !== 'CODE1' && $regcode !== '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 );
    
    function my_after_save_registration_details( $user_id, $submitted ) {
        global $regcode;
        $user = new WP_User( $user_id );
    	if ( $regcode == 'CODE1' ) {
    		$user->remove_role( 'subscriber' );
    		$user->add_role( 'role1' );
    	}
    		elseif ($regcode == 'CODE2') {
    		$user->remove_role( 'subscriber' );
    		$user->add_role( 'role2' );
    		}
    }
    
    add_action( 'um_after_save_registration_details', 'my_after_save_registration_details', 10, 2 );

    The first function sets and verifies the codes. You would replace ‘CODE1’ or ‘CODE2’ with the codes you want to use. Theoretically, could have MANY codes, but would be best to handle more than a few in an array.

    The second function checks the entered code, removes the generic Subscriber role and updates to the desired role. Of course, replace ‘role1’ and ‘role2’ with the appropriate EXISTING roles.

    Why use this instead of separate registration forms or the role drop down field available in UM? First, it is not possible to hide a form for one type of registrant from the other type of registrant. Systems like UM, Adminimize, etc. rely on the user being logged in to tailor visibility to a specific role. Second, in my situation, I can’t really have the registrant selecting their role – that needed to be determined on criteria that the registrant is unaware of.

    Reread the docs a little closer on the “um_registration_user_role” hook. I still believe it should have been a viable option since it would change the role on the form depending on the code, but I couldn’t get it to work.

    • This reply was modified 4 years, 9 months ago by pgrafix.
    Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @pgrafix

    Thanks for letting us know how you resolved the issue.

    I am closing this thread now.

    Regards,

    I tried this code in my functions.php. I replaced role1 and role2. I did not replace CODE1 or CODE2 (what should those be?) and upon registration receive message ”Invalid Nonce.” No user was created.

    Thread Starter pgrafix

    (@pgrafix)

    gflower1:

    CODE1 and CODE2 are whatever registration codes you want to use for the role assignment conditions. Obviously, they need to be different to allow for the appropriate user role to be assigned at registration.

    As for the Invalid Nonce error I can only think of 2 things ATM that are going wrong for you:

    1. Make sure you aren’t logged in as any user when you try to register as a new user.
    2. Make sure the user role referenced in the script is for a user role that already exists – this script only removes the default Subscriber role from a new registration, then assigns a different role (it doesn’t create it) based on the code used.

    Thanks. I’m trying to set up a job board where you can sign up as either a Candidate or Employer. Both of those roles exist thanks to WP Job Manager. Would CODE1 be Candidate and CODE2 be Employer? (I really need to learn PHP).
    My site is sowegajobs.com if you wouldn’t mind taking a look?!

    Thread Starter pgrafix

    (@pgrafix)

    Looking at your registration page, you don’t need the code above since you already give the option to register as either a Candidate or an Employer.

    The code I posted has two primary purposes:
    1. It prevents anyone from registering unless they have a registration code (the um_custom_validate_username function).
    2. Prevents a registrant from choosing a role at registration and instead removes the default role (set in the WP settings) and then has their role reassigned based upon the registration code they used. (my_after_save_registration_details function).

    I needed this for a very particular setup that involved two unconnected sites.

    Are you no longer wanting to give registrants the choice of Candidate or Employer?

    The option at registration is provided by Ultimate Member. I edited the Default Registration Form to include that option but it doesn’t affect the actual user role type after registration. Once registered, the user role is set to “none”. I definitely want to keep that option there but its not doing what I was expecting it to do.

    After further investigation, that form field only applies to UM Custom Roles. The Candidate and Employer were set by another plugin, therefore can’t be used in my situation.

    Thread Starter pgrafix

    (@pgrafix)

    Have you removed my code? If so, I would do that first before retesting.

    What are you settings for user registration under Settings>General?

    Thread Starter pgrafix

    (@pgrafix)

    Is it possible to have the other plugin use roles set my UM? If so, then you should be able to create the roles via UM and then select them under the other plugin. User roles are generally available site wide from my understanding.

    I did. Default registration is set to Candidate. Thanks for your help, I think I’m heading down a different rabbit hole altogether now unless I can merge UM Custom Roles into existing user roles.

    That’s a good idea. I don’t know enough about code to find out but I’m sure the plugin dev would!

    Thread Starter pgrafix

    (@pgrafix)

    Do you really need UM or will WPJM accomplish everything you need?

    You can change the employer role to any other role (i.e. one created by UM) from Job Listings>Settings>Job Submission menu…
    https://wpjobmanager.com/document/the-employer-role/

    Then change WP default to custom UM role as well…

Viewing 15 replies - 1 through 15 (of 16 total)
  • The topic ‘Set User Role at Registration’ is closed to new replies.