How To Set WordPress User Role Based On Email Domain

Published: 10 months ago

This tutorial will show you how to set WordPress user roles based on their email address.

This could be really helpful if you're looking to classify users based on the organizations that they're coming from. For example, student email addresses all end with a .edu extension, government email addresses all end with a .gov extension, and many companies utilize a standard domain across all of their employees. Therefore, if you're looking to have users sign up on your website and be classified into a specific user role based on their email address, you can do that with the following code snippet, sourced from

In this basic example, anybody with a email address is registered as an editor. Anybody with a email address becomes registered as a contributor.

add_action( 'user_register', 'iso_set_role_by_email' );
function iso_set_role_by_email( $user_id ){
    $user = get_user_by( 'id', $user_id );
    $domain = substr(
        ), 1
    ); //Get Domain

    $editor_domains = array( '' );
    if( in_array( $domain, $editor_domains ) ){
        foreach( $user->roles as $role )
            $user->remove_role( $role ); //Remove existing Roles
        $user->add_role( 'editor' ); //Add role

    $contributor_domains = array( '' );
    if( in_array( $domain, $contributor_domains ) ){
        foreach( $user->roles as $role )
            $user->remove_role( $role ); //Remove existing Roles
        $user->add_role( 'contributor' ); //Add role


Easily add this PHP snippet to your site with Advanced Scripts or Code Snippets. Just past in, hook into the init, and it will work like a charm.

Customizing this snippet is also very easy. All you need to do is replace the email domains with your custom domains, and then change the user role if you want to change that. You can even use a custom user role if you have added it to your site (“student” for example).

Change this email:     $contributor_domains = array( '' );
Change this role:         $user->add_role( 'contributor' ); //Add role
Join Our Newsletter
Here's what you get: A once a week newsletter that contains original content, aggregated posts, WordPress deals, and anything else we think you'd like. We don't spam or sell your email address. Unsub at any time.
Notify of
Inline Feedbacks
View all comments
IsoGroup- Web Dev/Design, WordPress and More

Affiliate Disclosure: Some links on this website are affiliate links. We make commissions for purchases made through those links.

We only recommend items/tools that we've personally used and like.

Full Disclosure | FTC Statement

Join Our Newsletter
Over 4,000 Design Lovers and Problem Solvers.


A relationship driven website creation company.


© ISOTROPIC, LLC 2016-2021
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram