strangerstudios / pmpro-roles

Adds a custom WordPress Role for each membership level in Paid Memberships Pro
https://www.paidmembershipspro.com/add-ons/pmpro-roles/
GNU General Public License v2.0
17 stars 16 forks source link

Enhancement: Add Role to Members List Table #43

Closed kimcoleman closed 5 months ago

kimcoleman commented 10 months ago

Is your feature request related to a problem? Please describe. It would be nice for this Add On to add the "Role" as a column on the Members list table.

Describe the solution you'd like Add Role as a column on the Members List

Describe alternatives you've considered Using the Users list.

kimcoleman commented 10 months ago

This is working code that could be further working on:

// Add the 'Role' column to the members list table
function add_role_column_to_members_list( $columns ) {
    $columns['role'] = __( 'Role', 'your-text-domain' );
    return $columns;
}
add_filter( 'pmpro_manage_memberslist_columns', 'add_role_column_to_members_list' );

// Output the roles for each user in the members list
function add_role_column_content_to_members_list( $column_name, $user_id ) {
    global $wp_roles;
    if ( 'role' === $column_name && !empty($wp_roles->roles) ) {
        $user = get_userdata( $user_id );
        $roles = $user->roles;

        // Get the display name for each role
        $role_names = array_map(function($role) use ($wp_roles) {
            return isset($wp_roles->roles[$role]['name']) ? translate_user_role($wp_roles->roles[$role]['name']) : ucfirst($role);
        }, $roles);

        echo implode( ', ', $role_names );
    }
}
add_action( 'pmpro_manage_memberslist_custom_column', 'add_role_column_content_to_members_list', 10, 2 );