# Email Users When Their Role Changes

> Sends users a short email when an administrator changes their role, for example when a subscriber becomes an author.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: users, roles, email
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/email-on-role-change/

## Code

```php
add_action( 'set_user_role', function ( $user_id, $role, $old_roles ) {
    if ( empty( $old_roles ) || in_array( $role, (array) $old_roles, true ) ) {
        return; // New registrations and unchanged roles.
    }

    $user  = get_userdata( $user_id );
    $names = wp_roles()->get_names();

    if ( ! $user || empty( $names[ $role ] ) ) {
        return;
    }

    $site = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );

    wp_mail(
        $user->user_email,
        sprintf( '[%s] Your role has changed', $site ),
        sprintf( "Hello %s,\n\nYour role on %s is now: %s.\n\nLog in: %s", $user->display_name, $site, translate_user_role( $names[ $role ] ), wp_login_url() )
    );
}, 10, 3 );
```

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Email Users When Their Role Changes" 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/
