Changing User Roles

By: Walter Jones | Asked: 02/15/2023
ForumsCategory: General questionsChanging User Roles
Walter JonesWalter Jones asked 2 years ago
For some reason I can't seem to find the right code that allows the changing of a wordpress user role from the formidable registration form on edit based on a drop down box on the registration form. Victor, I tried your mastermind tutorial, but it seems to just add the dropdown option as a capability instead of changing the actual user role. Also the FF forms version does the same thing, it adds a new capability instead of changing the role.
Bobby Clapp replied 2 years ago

Not sure I can help, but do have a question. Is the edit form the same form with the action that creates a user?

Walter JonesWalter Jones replied 2 years ago

Yes as it stands currently

Michael ClarkMichael Clark replied 1 year ago

Q: You have an admin View that lets you change the role, or maybe a param to ghange via link (URL)?

2 Answers
Mummy NinjaMummy Ninja answered 1 year ago
Same issue with Walter. It adds a new capability instead of changing a role. I'm trying to play around with the custom function to change the role instead of adding an additional role in the frontend.
Mummy NinjaMummy Ninja answered 1 year ago

I have something like this, it removes all capabilities before adding a new role. You can modify this to achieve what you want when changing roles on frontend.

add_action('frm_after_update_entry', 'update_user_role', 10, 2);
function update_user_role($entry_id, $form_id){
    if ( $form_id == 2 ) {
        
        $userid = $_POST['item_meta'][19];// ID of the userID field
        $role = $_POST['item_meta'][236];// ID of the role dropdown
        $accesslevel = $_POST['item_meta'][270];// ID of the role dropdown
            
        if ( $userid && $role ) {
            $user = get_userdata( $userid );
            if ( $user && ! $user->has_cap('administrator') ) {
             	$user->remove_all_caps();
                $user->set_role( $role );
              	$user->add_cap( 'access_level' , $accesslevel); 
            }
        }
    }
}

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right