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.

PHP
// 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. Its total_users key 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 uses return.
  • 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
<?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.

Share on X (opens in a new tab) Share on LinkedIn (opens in a new tab) Share on Facebook (opens in a new tab)

ChatGPT (opens in a new tab) Claude (opens in a new tab) Gemini (opens in a new tab) Perplexity (opens in a new tab) Grok (opens in a new tab)

More Insights

See More