# Member Count Shortcode

> Adds a [wpide_member_count] shortcode that shows how many members your site has, optionally for one role, cached so it stays fast on big sites.

- Type: PHP
- Runs: Run everywhere
- Category: [Users](https://wpide.com/library/category/users/)
- Tags: members, shortcode, statistics
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/member-count-shortcode/

## Code

```php
add_shortcode( 'wpide_member_count', function ( $atts ) {
    $atts   = shortcode_atts( array( 'role' => '' ), $atts, 'wpide_member_count' );
    $counts = get_transient( 'wpide_member_counts' );

    if ( false === $counts ) {
        $counts = count_users();
        set_transient( 'wpide_member_counts', $counts, 12 * HOUR_IN_SECONDS );
    }

    $role  = sanitize_key( $atts['role'] );
    $total = $role ? ( $counts['avail_roles'][ $role ] ?? 0 ) : $counts['total_users'];

    return esc_html( number_format_i18n( $total ) );
} );

$wpide_member_count_flush = function () {
    delete_transient( 'wpide_member_counts' );
};
add_action( 'user_register', $wpide_member_count_flush );
add_action( 'deleted_user', $wpide_member_count_flush );
add_action( 'set_user_role', $wpide_member_count_flush );
```

## Notes

Use [wpide_member_count role="subscriber"]. The count refreshes when someone registers or is deleted, and at least twice a day.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Member Count Shortcode" 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/
