Show Total Users In WordPress

Quick Summary
- Wrap count_users() in a function that returns total_users, the count across all roles, and register it as the user_count shortcode.
- Shortcode callbacks must return their output rather than echo it, which is why wpb_user_count() uses return.
- Add the shortcode with a Shortcode block, or use do_shortcode() in templates, and write your own wording since it outputs only the number.
To show how many users are registered on your WordPress site, for example on a community or membership page, you can wrap the core count_users() function in a shortcode. Add the first snippet to your child theme’s functions.php or a custom plugin.
// Function to return user count
function wpb_user_count() {
$usercount = count_users();
$result = $usercount['total_users'];
return $result;
}
// Creating a shortcode to display user count
add_shortcode('user_count', 'wpb_user_count');How it works
count_users()returns an array with information about the site’s users. Itstotal_userskey holds the number of users across all roles.wpb_user_count()returns that number. Shortcode callbacks must return their output rather than echo it, which is why the function usesreturn.add_shortcode()registers[user_count]and connects it to the function.
Display the count
In a post or page, add [user_count] where the number should appear, using a Shortcode block in the block editor or typing it directly in the classic editor. In a theme template file, run the shortcode with do_shortcode():
<?php echo do_shortcode('[user_count]'); ?>Inside a template you can also call wpb_user_count() directly, which skips the shortcode step. The shortcode outputs only the number, so add your own wording around it.
Related articles
- a current year shortcode for your footer: another small shortcode built the same way
- a sortable registration date column on the Users screen: more user information in the admin

