Select role from register page

This topic contains 3 replies, has 2 voices, and was last updated by  Alex Howes 6 years, 6 months ago.

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket
  • Author
    Posts
  • #2140

    Alex Howes
    Buyer
    Post count: 175

    Hello,

    I’d like users registering to be able to select their role in the registration form, and then have their role changed to that role on registration.

    I’ve written some basic code as a snippet, but not sure which hook I need to use to hook into the registration form submit?

    Also not sure how to access parts of the form- would this be using global $post, and $POST[‘role’] == ‘role’?

    Thanks for your help. I realise this is customization which is beyond the scope of support but would be grateful if you could point me in the right direction 🙂 Also seems like quite a few people are looking for a way to do this.

    Any idea when the usersWP roles add-on will be available?

    Thanks 🙂 Alex

    #2141

    Alex Howes
    Buyer
    Post count: 175

    My current code is:

    add_filter(‘hook’, ‘assign_user_role_on_registration’);

    function assign_user_role_on_registration( $data ) {

    global $post;
    $post_id = $post->ID;

    if ($post_id) {
    $role = geodir_get_post_meta($post_id, ‘role’);

    $current_user = wp_get_current_user();
    $current_user_roles = $current_user->roles;

    if (in_array(‘subscriber’, $current_user_roles)){

    $current_user->set_role( $role );

    }
    }
    return $data;
    }

    Where hook is the relevant hook (not sure what this is). I’m also not sure if geodir_get_post_meta is the right way to access the form field.

    #2148

    Patrik
    Moderator
    Post count: 1971

    Hi Alex,

    Go to the file plugins/userswp/includes/class-forms.php at line no. 341 and you will see following action which you can use to process the form data and assign role to the user as you will get $data and $user_id as a parameter to use.

    do_action('uwp_after_custom_fields_save', 'register', $data, $result, $user_id);

    Let me know if this helps you!

    Regards,
    Patrik

    #2149

    Alex Howes
    Buyer
    Post count: 175

    Thanks Patrik

    It’s now working 🙂 In case anyone else on here is interested in doing this, I’ve written my code below. The html variable name for the role field is ‘role’ which must be prefixed by ‘uwp_account_’. The two options it can take on my form are ‘Worker’ and ‘Employer’ which correspond to my two roles ‘um_worker’ and ’employer’.

    
    
    add_action('uwp_after_custom_fields_save', 'assign_user_role_on_registration', 10, 4);
    
    function assign_user_role_on_registration( $form_type, $data, $result, $user_id ) {
      
      if ($form_type == 'register') {
    	
            $role = $data['uwp_account_role'];	
    		$current_user = get_userdata($user_id);
    	    
    	    if ($current_user) {
    	
    			$current_user_roles = $current_user->roles;
    	
    			if (in_array('subscriber', $current_user_roles)) {
    		  
    		  		if ($role == 'Worker') {
    	  
    	  				$current_user->set_role( 'um_worker' );	
    		    	}
    		  
    		  		if ($role == 'Employer') {
    		   
    		    		$current_user->set_role( 'employer' );			  
    		    	}
      	  		 }
    	   }
      }
    }
Viewing 4 posts - 1 through 4 (of 4 total)

We have moved to a support ticketing system and our forums are now closed.

Open Support Ticket