# Allow the Administrator Role Only for Company Email Addresses

> Refuses to give the administrator role to accounts whose email address is not on one of your approved domains, whether set from the profile screen, the Add User screen or a bulk role change.

- Type: PHP
- Runs: Run everywhere
- Category: [Security](https://wpide.com/library/category/security/)
- Tags: administrators, roles, email
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/sec-admin-role-email-domains/

## Code

```php
// Email domains allowed to hold the administrator role.
$wpide_admin_domains = array( 'example.com' );

$wpide_domain_ok = function ( $email ) use ( $wpide_admin_domains ) {
    $domain = strtolower( (string) substr( strrchr( (string) $email, '@' ), 1 ) );
    return in_array( $domain, array_map( 'strtolower', $wpide_admin_domains ), true );
};

add_action( 'user_profile_update_errors', function ( $errors, $update, $user ) use ( $wpide_domain_ok ) {
    if ( isset( $user->role ) && 'administrator' === $user->role && ! $wpide_domain_ok( $user->user_email ) ) {
        $errors->add( 'wpide_admin_domain', '<strong>Error:</strong> Administrators must use an approved company email address.' );
    }
}, 10, 3 );

add_action( 'set_user_role', function ( $user_id, $role, $old_roles ) use ( $wpide_domain_ok ) {
    $user = get_userdata( $user_id );

    if ( 'administrator' !== $role || ! $user || $wpide_domain_ok( $user->user_email ) ) {
        return;
    }

    static $busy = false;
    if ( $busy ) {
        return;
    }

    $busy     = true;
    $fallback = $old_roles && 'administrator' !== reset( $old_roles ) ? reset( $old_roles ) : get_option( 'default_role', 'subscriber' );
    $user->set_role( 'administrator' === $fallback ? 'subscriber' : $fallback );
    $busy = false;
}, 1, 3 );
```

## Notes

Set $wpide_admin_domains to your own domains before activating; existing administrators are not changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Allow the Administrator Role Only for Company Email Addresses" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
