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:
- Navigate to Appearance → Widgets.
- Look for the 'Legacy Widget' block if you are using a block theme.
- Add the Legacy Widget and select either the 'Members: Login Form' or 'Members: Users' widget from the dropdown.
- 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.phpfile 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
-
How to activate Google Sitekit?https://wordpress.org/support/topic/how-to-activate-google-sitekit/
-
A login form widget and users widget to show in the theme’s sidebars.https://wordpress.org/support/topic/a-login-form-widget-and-users-widget-to-show-in-the-themes-sidebars/
-
How to display the name of the role assigned to a user?https://wordpress.org/support/topic/how-to-display-the-name-of-the-role-assigned-to-a-user/
-
Where is widgetshttps://wordpress.org/support/topic/where-is-widgets-2/
-
Users Widget – Remove link when listing usershttps://wordpress.org/support/topic/users-widget-remove-link-when-listing-users/
-
role listhttps://wordpress.org/support/topic/role-list/
-
What’s the PHP code to display logged in user’s Role?https://wordpress.org/support/topic/whats-the-php-code-to-display-logged-in-users-role/
-
show / hide widget based on rolehttps://wordpress.org/support/topic/show-hide-widget-based-on-role/
-
Widget with a list of the members of a rolehttps://wordpress.org/support/topic/widget-with-a-list-of-the-members-of-a-role/
-
Show Username in Login Widgethttps://wordpress.org/support/topic/show-username-in-login-widget/
-
Login Widget Where?https://wordpress.org/support/topic/login-widget-where/
-
Display user role in my accounthttps://wordpress.org/support/topic/display-user-role-in-my-account/
-
members avec u bloc themehttps://wordpress.org/support/topic/members-avec-u-bloc-theme/
-
list users shortcodehttps://wordpress.org/support/topic/list-users-shortcode/