User Registration (User Update Feed) - Let the Form determine the Role, not the Feed [RESOLVED]

Hi guys!

I have a form that updates user meta upon submission (via the User Update Feed of the User Registration add-on). One of the values it updates is the role assigned to the user. My problem is that I want the form to assign one of two roles to the user, depending on a choice field selection in the form.

Would it work if I set Role to “Preserve current role” under User Settings and then, under User Meta, select wp_user_level or role to be updated by the choice field in question (the options of which correspond with existing roles)? Is wp_user_level or role the correct meta to use, or should I create a role meta? And won’t the “Preserve current role” setting prevent the role from being updated?

Looking forward to hearing the awesome wisdom of the community! :grin:

Cheers,
Phil

You can use a little snippet to set the role based on the user selection (e.g. using a drop down field).

What you select in the feed settings for the role doesn’t matter, because the snippet will override the role with the selected one in the form field.

1 Like

Hey Samuel, I mentioned awesome wisdom and I wasn’t disappointed! :smile::+1: Thanks so much for that, I’ll give it a whirl.

Best,
Phil

1 Like

Two questions, if you don’t mind… is there a way to specify the form ID in that snippet, and will this actually update the current role to the new one? I.e. it’s NOT a new user but an existing one?

Oops! I sent you the filter for the new user registration by mistake :blush:

The same thing can be done with the gform_user_updated filter that runs only for existing users updates, there’s also an example in this filter doc page: https://docs.gravityforms.com/gform_user_updated/#update-user-role

You can limit the snippets to a concrete form only by checking the form id at the start of the function, for example:

// Run only for form id 1
$form = GFAPI::get_form( $entry['form_id'] );
if ( $form['id'] != 1 ) {
   return;
}

Great, thanks Samuel - I’ll give it a go. Appreciate the effort!

Sorry for bugging you again - I’m having a dunce moment here - how would I add the “Run only for form id …” bit to the rest of the snippet?

You’re welcome Phil, by the way, I forgot to add a line to the above snippet to get $form first, sorry it’s 3:00 AM here)…

I have edited the snippet now.

LOL… I’m sitting at 04:00 AM, so I’m not focusing much.

So sorry, but I still don’t really know where to add the above snippet… :sweat_smile:

In relation to the gform_user_updated snippet, that is…

At the start of the function:

add_action( 'gform_user_updated', 'change_role', 10, 4 );
function change_role( $user_id, $feed, $entry, $user_pass ) {

// Run only for form id 1
$form = GFAPI::get_form( $entry['form_id'] );
if ( $form['id'] != 1 ) {
   return;
}

// Any other code goes below this line...
1 Like

Aah - thanks a bunch Samuel, much appreciated!

Now I’m off to bed! :sleeping: