[MEMBERSHIP] user roles

We are using a plugin called “Admin Columns” to give our client the ability to go to Users > All Users and see/filter by WordPress fields and Buddypress extended profiles.

This works great.

However, the field that shows the Membership from Membership Pro 2 doesn’t support filtering and can’t be exported to the CSV that Admin Columns enables.

As a workaround, we were hoping to have the Membership plugin change the role based on the membership status so we could use that field instead.

I think https://premium.wpmudev.org/forums/topic/assign-roles-to-members-with-membership-2 might do what we need. But I don’t think that would revert them back to a different role if they were to unsubscribe.

  • Nithin
    • Support Wizard

    Hi Moez Tharani,

    I got an update regarding this from the developer, this could be possible. However, we would like to confirm the intended workflow you are looking to achieve.

    That’s, change the role upon user registration, and also check existing members and change role when member’s subscription gets expired or cancelled? Is that the workflow you are looking, right?

    Please do confirm so that we could help get you sorted. Have a great day ahead. :slight_smile:



  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Moez Tharani and sorry for the late reply.

    In order to alter the user roles depending on the membership status you can add this mu-plugin:

    How to install:

    Always make sure to keep a backup of your site before changing/adding custom code.

    1] Navigate to your /wp-content/ directory and create a new one named mu-plugins if it doesn’t exist.

    2] Inside the mu-plugins folder create a file named add-membership-role.php

    3] Edit the file and copy / paste this code snippet inside.


    function( $username, $user ) {
    $member = MS_Factory::load( 'MS_Model_Member', $user->ID );

    if ( $member->has_membership() ) {
    $user->set_role( 'editor' );
    } else {
    $user->set_role( 'subscriber' );

    4] Edit the “editor” and “subscriber” in the code depending on the roles that you want to adjust on each occasion.

    5] Save and close the file.

    6] The final path should look like /wp-content/mu-plugins/add-membership-role.php

    As a note this check for this particular mu-plugin will happen on each user login. This would be the most “less expensive” in resources to use but please tell me if you want me to create a script for you to check all members daily for example with a scheduled task as well and adjust their roles as well as a batch process.



  • Moez Tharani
    • New Recruit

    We would need this to run on a regular basis. The issue is that the client is going to use this for exporting/reporting, so being able to quickly identify who are members and who aren’t is vitally important. Due to the nature of the subscription, members may login infrequently or never.

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hi Moez Tharani ,

    I’ve update the code to create a more refined scheduled task for your needs. This will set a daily scheduled task that will check all users and adjust their role respectively depending on what you define to it. It will automatically exclude all the administrators as there’s no role-change needed for them. It will also add some debugging information if it’s enabled adding some lines into debug.log telling you which members have changed each time and when the process ends just in case you want to inspect it at some point.

    The edits that you can make are :

    wp_schedule_event( time(), 'daily', 'cron_check_membership_roles' );

    On the above line you can change the ‘daily’ into ‘twicedaily’ or ‘hourly’ as well depending on how many times you want the script to run each day. I wouldn’t recommend doing an ‘hourly’ as it will be a bit of an expensive script and it might need some resources depending on how many users the website actually has.

    You should also change the:

    $role_with_membership    = 'editor';
    $role_without_membership = 'subscriber';

    To adjust the roles that you want to assign in each case ( if there’s a membership or not ).

    Everything else should be working automatically but as always do keep a backup first before giving this a try to be on the safe side :slight_smile: .

    Please tell me if you need any further help or adjustments with this!

    * Plugin Name: Membership 2 Pro - Scheduled task to update user roles.
    * Plugin URI: https://premium.wpmudev.org/
    * Description: Adds a cronjob and check if users have membership to update their roles accordingly.
    * Version: 1.0.0
    * Author: Konstantinos Xenos @ WPMUDEV
    * Author URI: https://premium.wpmudev.org/
    * License: GPLv2 or later

    if ( ! wp_next_scheduled( 'cron_check_membership_roles' ) ) {
    // you can set to hourly, twicedaily, daily
    wp_schedule_event( time(), 'daily', 'cron_check_membership_roles' );

    add_action( 'cron_check_membership_roles', 'wpmudev_cron_check_membership_roles' );
    function wpmudev_cron_check_membership_roles() {

    // Change the roles depending on your preference.
    $role_with_membership = 'editor';
    $role_without_membership = 'subscriber';

    // Do not edit after this line.
    $args = array(
    'role__not_in' => array( 'administrator' ),

    $users = get_users( $args );

    foreach ( $users as $user ) {
    $member = MS_Factory::load( 'MS_Model_Member', $user->ID );

    if ( ! in_array( $role_with_membership, $user->roles, true ) && $member->has_membership() ) {
    $user->set_role( $role_with_membership );

    if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
    error_log( '=== Changing role for user ' . $user->user_login . ' (' . $user->ID . ') ' . 'to ' . $role_with_membership );
    } elseif ( ! in_array( $role_without_membership, $user->roles, true ) && ! $member->has_membership() ) {
    $user->set_role( $role_without_membership );

    if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
    error_log( '=== Changing role for user ' . $user->user_login . ' (' . $user->ID . ') ' . 'to ' . $role_without_membership );

    if ( defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
    error_log( '=== Membership roles check finished.' );



Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.