• Resolved pete2650

    (@pete2650)


    Hi guys

    My workflow is

    User registers. They get assigned a role of User.

    They can then choose to upgrade to role of Agent (which requires them to upload their drivers license).

    I’ve created two rego forms but that doesn’t work as when a User Role try’s to register as an Agent Role they are already logged in.

    How would I solve this?

    Cheers

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Contributor Champ Camba

    (@champsupertramp)

    Hi @pete2650

    You can try the following code snippets to your theme/child-theme’s functions.php file or use the Code Snippet to run the code:

    add_action( 'profile_update', function( $user_id, $old_user_data ){
        if( is_admin() ) return; 
        $keys = array_keys( $_REQUEST );
    
        if( ! class_exists('UM') ) return;
    
        $user   = get_userdata( $user_id ); // get the user data
        $user_roles = UM()->roles()->get_all_user_roles( $user_id ); // get all user roles
        foreach ( $user_roles as $role ) { 
            $user->remove_role( $role ); // remove all roles
        }
    
        foreach( $keys as $k ){
            if( get_user_meta( $user_id, 'drivers_license', true ) ){
               
                $role_slug = 'um_member-patreon'; // User Role slug
                
                UM()->roles()->set_role( $user_id, $role_slug ); // Set the new role
                
            }else{ // if no driver's license, assign the subscriber role.
                $role_slug = 'subscriber'; // User Role slug
                
                UM()->roles()->set_role( $user_id, $role_slug ); // Set the new role
                
            }
        }
    
        UM()->user()->remove_cache( $user_id ); // remove cache
            
        
    }, 9999, 2 );

    The above code will assign a role um_member-patreon when there’s a driver’s license uploaded in the profile form. If there’s no uploaded image file, it will assign a subscriber role to the user. The assigning of roles occurs when you submit the Profile Form. Please take note that it will remove all roles of a user and then assign a new role to the user when updating the role so make sure that you test it with another non-administrator account.

    Regards,

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

    (@champsupertramp)

    Hey there!

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread by changing the Topic Status to ‘Not Resolved’ if any other questions come up and we’d be happy to help. ??

    Regards,

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘User upgrade role’ is closed to new replies.