Apply multiple Gravity Forms User Registration Feeds at the same time (Assign multiple roles to new users)

The following code is an example of how you can assign multiple roles to one user during registration using the Gravity Forms User Registration Add-on.

By default, Gravity Forms will apply only the first “User Registration Feed” that meets the conditions, and you can assign only one role to it. With this code, the validation process will continue and if you meet the criteria of the next feed, the user will get that role as an additional role (and again and again for each feed).

				
					/**
 * Assign multiple roles to new users based on checkbox.
 */
add_action( 'gform_user_registered', 'gf_add_another_role', 10, 3 );

function gf_add_another_role( $user_id, $feed, $entry ) {
	$user = new WP_User( $user_id );
	$feeds = GFAPI::get_feeds( array(
		3,
		4,
		5,
		6,
	) );
	$field = GFAPI::get_field( 2, 10 );
	$values = array();
	foreach ( $field->inputs as $input ) {
		if ( ! empty( $entry[ $input['id'] ] ) ) {
			$values[] = $entry[ $input['id'] ];
		}
	}

	foreach ( $feeds as $feed ) {
		foreach ( $feed['meta']['feed_condition_conditional_logic_object']['conditionalLogic']['rules'] as $rule ) {
			if ( in_array( $rule['value'], $values ) ) {
				$user->add_role( $feed['meta']['role'] );
				break;
			}
		}
	}
}
				
			

What this code example does:

Checks if the user checked the checkboxes to meet the criteria for feeds 3, 4, 5 and 6. And will assign the user role of each feed that is fulfilled.

The logic of what checkboxes need to be checked for each role are set on each User Registration feed, not on this code. This code only helps to continue the execution instead of stoping when the first role is assigned.

Powered by BetterDocs

Newsletter

Subscribe and stay connected through our Newsletter. We send out important news, tips and special offers.

  • This field is for validation purposes and should be left unchanged.