I need to add multiple roles using gravity form ID 13 from subsite ID 11

I have been trying to create a user with multiple roles for when they sign up to gravity form ID 13 from subsite ID 30. I have tried creating 3 user feeds but only the first user feed is getting picked up by the website.

I also tried adding in a php code which is not working and it is:

function add_additional_role( $user_id, $feed, $entry, $user_pass ) {
if ( 11 !== get_current_blog_id() )
return;
// Role name ID to add
$role_to_add =array("ra_bronze_member","affiliate","bronze_member":wink:;
// Get current user object
$user_obj = new WP_User( $user_id );
// Add the role above to existing role(s) for the user
$user_obj->add_role( $role_to_add );
}

I have added the website ID because I am using multisite and I there are different user role to be added in different sites.

The second one I need to do is to add additional roles to who is already signed up in the website ID 30 using the same form ID 13. So what I did with the form is a hidden field "Already Logged In?" and using a conditional field of hiding the need for a username and a password. I would like to add the roles biz_bronze_member, affiliate and bronze_member. Is there a different code for this one?

  • Zandra Flores
    • WPMU DEV Initiate

    I had a thought and maybe it would be easier if let say...

    A user sign up and was created with a user role of 'biz_bronze_member' then I can add a php action where if this is a case two more roles are added to that specific role which is 'affiliate' and 'bronze_member'. Is this doable please? Thanks in advance.

  • Huberson
    • Recruit

    Hello Zandra Flores
    You could try looping through the roles array and assign each role(string) to the user, instead of passing the array to "add_role( )".

    Basically, you will check the blog ID like in your above code, and check rather the user has the specific role you need.
    Something like this to get the role:

    $user = wp_get_current_user();
    $role = ( array ) $user->roles;

    Then assign those roles to the user if her current role matches "biz_bronze_member" :

    ...
    $user_obj = new WP_User( $user_id );
    $role_to_add = array("ra_bronze_member","affiliate","bronze_member");
    foreach ($role_to_add as $role) {
    	$user_obj->add_role($role);
    
    }
    ...

    There's also a workaround from Gravity Wiz site you can look at regrading the initial query - Dynamically Populating User Role

    Note, I didn't fully implement what you mentioned above so you will need to test before applying it on live site.

    Regards,
    Huberson

  • Zandra Flores
    • WPMU DEV Initiate

    Just some code I tried with suggestions from wpmu support so we do not get multiple suggestions:

    This one crashed my site.

    if ( is_user_logged_in() )
    {
    global $current_user;
    $user_role = $current_user->roles[0];
    if($user_role == 'biz_bronze_member')
    {

    add_action( 'gform_user_updated_13', 'add_additional_role', 10, 4 );
    function add_additional_role( $user_id, $feed, $entry, $user_pass ) {

    // Role name ID to add
    $role_to_add = 'affiliate';

    // Get current user object
    $user_obj = new WP_User( $user_id );
    $user_obj->add_role( $role_to_add );
    }
    }
    }

  • Zandra Flores
    • WPMU DEV Initiate

    This one just did not work:

    if ( is_user_logged_in() )
    {
    global $current_user;
    $user_role = $current_user->roles[0];
    if($user_role == 'biz_bronze_member')
    {

    add_action( 'gform_user_updated_13', 'add_additional_role', 10, 4 );
    function add_additional_role( $user_id, $feed, $entry, $user_pass ) {

    // Role name ID to add
    $role_to_add =array("affiliate","bronze_member");

    // Get current user object
    $user_obj = new WP_User( $user_id );
    $user_obj->add_role( $role_to_add );
    }
    }
    }

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.