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.
Opens WPIDE on
Code
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 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.