# Users by Role Dashboard Widget

> Shows how many users the site has in total and per role, with links to each filtered users list.

- Type: PHP
- Runs: Admin only
- Category: [Dashboard](https://wpide.com/library/category/dashboard/)
- Tags: dashboard, widget, users
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/users-by-role-dashboard-widget/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Users by Role Dashboard Widget" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
