Users by Role Dashboard Widget
Shows how many users the site has in total and per role, with links to each filtered users list.
Opens WPIDE on
Code
add_action( 'wp_dashboard_setup', function () {
if ( ! current_user_can( 'list_users' ) ) {
return;
}
wp_add_dashboard_widget( 'wpide_users_by_role', 'Users', function () {
$counts = count_users();
$names = wp_roles()->get_names();
printf( '<p><strong>%s</strong> users in total.</p><ul>', esc_html( number_format_i18n( $counts['total_users'] ) ) );
foreach ( $counts['avail_roles'] as $role => $count ) {
if ( ! $count || 'none' === $role ) {
continue;
}
printf(
'<li><a href="%1$s">%2$s</a>: %3$s</li>',
esc_url( admin_url( 'users.php?role=' . $role ) ),
esc_html( isset( $names[ $role ] ) ? translate_user_role( $names[ $role ] ) : $role ),
esc_html( number_format_i18n( $count ) )
);
}
echo '</ul>';
} );
} );
Notes
count_users() can be slow on sites with hundreds of thousands of users.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.