A client recently contacted me searching for an easy way to automatically assign a specific role to a registering user based on the domain (i.e. gmail.com) of the user’s email address (i.e. firstname.lastname@example.org).
If the registering user’s email domain was a part of his organization, that user would be assigned to an “Employee” role. All other registering users would be assigned to the generic “Subscriber” role.
Fortunately, the registration form was powered by the Gravity Forms User Registration add-on which makes this super easy to accomplish.
In this tutorial, I will show you how to use multiple User Registration feeds to assign specific user roles dependent on the email domain of the registering user. Let’s get started!
You’ll need a few things to get started.
- Gravity Forms (Developer License)
- Gravity Forms User Registration Add-On
- About 15 minutes
Ready to walk through each step?
- Step 1: Set up the registration form in Gravity Forms
- Step 2: Configure the form to create user accounts
- Step 3: Create a User Registration Feed for Each User Role
Step 1: Setting Up the Registration Form
First things first, we need to set up the form that will be used to register users for the site. To create a simple version of this form, please follow the directions found here. If you follow these instructions you’ll wind up with a form that looks like this:
Now that we have our basic registration form, we need to set up the form to create new user accounts.
Step 2: Configure the form to create user accounts
The User Registration Add-on allows us to create feeds that map the Registration Form fields to WordPress User fields. So you simply need to connect the correct WordPress labels with the values from your form.
At the bottom of the Registration Feed, you will see a section called “Additional Options.” In this section there is an option called “Registration Condition.” This is where the magic happens.
Check the “Enable” checkbox to enable this option. A new option will be displayed that allows you to specify a condition for this registration feed. This means that if the condition is met, this feed will be used to register the user.
Here is an example of a condition which will match any user who registers using a “gravitywiz.com” email address. Based on the “Role” setting for this feed, such users would be assigned the “Subscriber” user role.
Step 3: Create a User Registration Feed for Each User Role
If there are email domains that should be assigned to specific user roles, create a separate feed with the corresponding “Registration Condition” for each email domain.
Now whenever a user registers from this form, Gravity Forms will find the first matching registration feed (depending on conditional logic) and register the user (and assign their role) based on the matching feed.
How’d we do?
If you found this tutorial helpful, let us know. Also, we’d love to hear the different ways that you used this technique. Thanks!
Did this resource help you do something awesome with Gravity Forms?
Then you'll absolutely love Gravity Perks; a suite of 46+ essential add-ons for Gravity Forms with support you can count on.
Brandon Leavitt says
Do you have to create a form for each role or is there a way to assign different roles with one form?
Scott Buchmann says
Multiple roles can be assigned with a single form. Gravity Forms will find the first matching registration feed and register the user with the corresponding role based on the matching feed.
If you would like to assign a user role based on a form choice from the registration form or update form (in this case a dropdown), it’s possible to use the following function:
David Smith says
Thanks for sharing, Lars!
Would this work with 1000s of conditions?
We want to supply codes to user in the field, when the enter the code I want to be able to register them as 1 of 3 roles? Possible? Just a lot of work.. lol Is there a better way?
David Smith says
Just getting to this comment, Sonya. Per our email conversation, a custom solution will be the way to go. Looking forward to working with you. :)
Thanks for this. I didn’t have this particular need, but reading this guide helped me to realize I could also probably require User Activation for certain email domains and not others. I have been having a lot of problems with Yahoo email addresses getting caught by their spam filter (using Mandrill) and not going through, so now it looks like I can give Yahoo users special treatment during the registration process.
Jordan Smith says
Hey Erik, glad you got some inspiration from this!