Assign A User Role Based On Email Domain With Gravity Forms

Use multiple User Registration feeds to assign different user roles based on the submitted email domain.

Introduction

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. john.smith@gmail.com).

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!

While this tutorial is focused on conditional user roles via User Registration feeds, this same concept applies to all Gravity Forms feed-based add-ons.

Getting Started

You’ll need a few things to get started.

Ready to walk through each step?

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.

user-registration-feed

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.

Gravity Forms User Registration Condition

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.

Want to create custom user roles? You can use Justin Tadlock’s Members Plugin to handle this easily!

Want to allow users to be automatically logged in once they register? Get the GP Auto Login plugin with the Gravity Perks package. Just click, install, and activate!

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!

Comments

    1. Scott Ryer
      Scott Ryer Staff December 11, 2020 at 4:25 pm

      Hi Brandon,

      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.

  1. Sonya
    Sonya January 22, 2015 at 11:08 am

    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?

    Reply
    1. David Smith
      David Smith Staff January 25, 2015 at 5:38 pm

      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. :)

  2. Weekly Roundup: October 24, 2014 – Sell with WP October 24, 2014 at 2:00 am

    […] Wiz posted a 15-min tutorial on assigning a user-role based on email domain with a Gravity Forms registration form (handy for giving different roles to your […]

    Reply
  3. Erik
    Erik October 23, 2014 at 4:33 pm

    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.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Grab a bundle of free Gravity Forms plugins

Enter your email and receive our most popular free plugins and snippets, plus access to hundreds of others.

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