Adding a role using Members and Formidable Forms

Question

Not a coder 🙁
Been using a snippet to change roles when someone completes a Formidable Forms. Works fine. But on another site, I want to just ‘add’ a role. I don’t know how to modify the snippet below to add a given role rather than changing it.
Thoughts?

/**
* This will change a user to a different member role after they complete their member profile.
*/
add_action('frm_after_create_entry', 'inactive_to_member', 20, 2);
function inactive_to_member($entry_id, $form_id){
 if($form_id == 21){ // form id of the form to copy
   $new_role="census_club"; //change this to the role users should be granted upon completing form

   $user = wp_get_current_user(); //get logged in user
   if(!$user) {
       return; //don't continue if user doesn't exist
   }

   $updated_user = (array)$user;

   // Get the highest/primary role for this user  
   $user_roles = $user->roles;
   $user_role = array_shift($user_roles);
   if ( $user_role == 'administrator' ) 
       return; //make sure we don't downgrade any admins

   $updated_user['role'] = $new_role;
    
   wp_update_user($updated_user);
 }
}```

0
TravelingTartar 2 weeks 2021-09-02T17:14:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse