• Resolved Giray

    (@giray)


    If anyone can help, great… ?? I’m out of my depth.

    Using Members on a site with Formidable Forms. FF have written a script that allows for a member to be changed role upon completion of a form. It works great. However, instead of ‘changing’ the role, I would just like to ‘add’ a role. If anyone can look at the code below and tell me what I need to do differently so that Members only adds the role rather than changing it, I would be sooooo grateful.
    If that’s not possible, another option is to add additional ‘no touch’ roles like the admin. That way, someone who already has higher permissions won’t get downgraded. I don’t know what code to add to add additional roles to the admin role failsafe below.
    Thanks

    /**
    * 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);
    }
    }

Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi Giray,

    Here’s an updated version of your code that adds the role and doesn’t remove any role:

    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
        }
    
        if( in_array('administrator', $user->roles) ) return; //make sure we don’t downgrade any admins
    
        $user->add_role( $new_role ); // Add role
      }
    }

    Hopefully, that helps.

    Thread Starter Giray

    (@giray)

    Thank you SO MUCH!!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add a second role with snippet’ is closed to new replies.