Back to Community

How to Display User Roles and Lists with the Members Plugin

Content

Many WordPress site administrators use the 'Members – Membership & User Role Editor Plugin' to manage user roles and capabilities. A common task is to display user role information or create lists of users with specific roles on the front end of a site, such as in a forum, a user dashboard, or a widget. This guide covers the most effective methods to achieve this.

Why Display User Roles and Lists?

Showing a user's role can help create a sense of community hierarchy, highlight staff members, or grant access to specific content. Listing users by role is useful for creating staff directories, showcasing contributors, or simply displaying a member list.

Common Solutions

1. Displaying the Current User's Role

To show the currently logged-in user their own role, you can use a custom shortcode. Add the following PHP code to your theme's functions.php file or a code snippets plugin:

function display_members_user_role() {
    if (is_user_logged_in()) {
        $current_user = wp_get_current_user();
        $roles = $current_user->roles;
        
        return !empty($roles) ? esc_html(implode(', ', $roles)) : 'No roles assigned';
    } else {
        return 'User not logged in';
    }
}
add_shortcode('members_user_role', 'display_members_user_role');

After adding this code, you can place the shortcode [members_user_role] in any post, page, or widget to display the role.

2. Creating a List of Users by Role

The plugin includes a 'Users' widget that can display a list of users based on their role. To use it:

  1. Navigate to Appearance → Widgets.
  2. Look for the 'Legacy Widget' block if you are using a block theme.
  3. Add the Legacy Widget and select either the 'Members: Login Form' or 'Members: Users' widget from the dropdown.
  4. For the 'Users' widget, configure the role and other parameters to generate your list.

If you need more control or want to use a shortcode, you can create a custom one. Add this code to your functions.php file:

add_shortcode('mb-display-users-role', function($atts) {
  if(!isset($atts['role'])) return;
  
  $users = get_users( array('role' => $atts['role']));
  if(empty($users)) return;
  
  echo '<ul>';
  foreach ( $users as $user ) {
    echo '<li>' . esc_html( $user->display_name ) . '</li>';
  }
  echo '</ul>';
});

You can then use the shortcode [mb-display-users-role role="subscriber"], replacing "subscriber" with your desired role.

3. Removing Links from the Users Widget List

By default, the Users widget links each name to its author archive page. If you wish to remove these links, you would need to customize the widget's output with additional code, as there is no built-in setting to disable them.

Important Notes

  • Always add custom code to a child theme's functions.php file or a dedicated code snippets plugin to prevent loss during theme updates.
  • The parameters for the Users widget (Include, Exclude, Search, Meta Key) correspond directly to the WordPress get_users() function.
  • For displaying content based on a user's role, the 'Members' plugin offers additional shortcodes that can be used to restrict content.

By following these methods, you can effectively display user roles and create customized user lists to enhance your WordPress site's community features.

Related Support Threads Support