# Paid Memberships Pro: Give Each Level Its Own Role

> Adds a WordPress role that matches the member's level when they join and removes it when they cancel, expire or change level.

- Type: PHP
- Runs: Run everywhere
- Category: [Memberships](https://wpide.com/library/category/memberships/)
- Tags: pmpro, roles, access
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/pmpro-level-role-sync/

## Code

```php
if ( ! function_exists( 'pmpro_hasMembershipLevel' ) ) {
    return;
}

// Level ID => role.
$wpide_pmpro_level_roles = array(
    1 => 'contributor',
    2 => 'author',
);

add_action( 'pmpro_after_change_membership_level', function ( $level_id, $user_id ) use ( $wpide_pmpro_level_roles ) {
    $user = get_userdata( $user_id );

    if ( ! $user || user_can( $user, 'manage_options' ) ) {
        return;
    }

    foreach ( $wpide_pmpro_level_roles as $level => $role ) {
        if ( ! get_role( $role ) ) {
            continue;
        }

        if ( pmpro_hasMembershipLevel( $level, $user_id ) ) {
            $user->add_role( $role );
        } elseif ( in_array( $role, (array) $user->roles, true ) && count( $user->roles ) > 1 ) {
            $user->remove_role( $role );
        }
    }
}, 10, 2 );
```

## Notes

The roles must exist. The member's main role is kept, and administrators are never changed.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Paid Memberships Pro: Give Each Level Its Own Role" 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/
