Do you want to display the total number of WordPress users on your entire site? Use the following code to get the total number of users, and then display it in your theme. This returns ALL users, regardless of User Level.
<?php // Get total number of users $total_users = count_users(); echo $total_users['total_users'] . ' users.'; ?>
The $total_users
variable will just return the number. You can change the word “users” to say whatever you want (registered users, people, etc.).
I’d love to see how you use this. Please share in the comments.
Reference count_users() in the WordPress Codex.
Harold / #
thanks for posting this..
Dave Warfel (Author) / #
Sure thing, Harold. I hope you found it useful.
Manny / #
Hey Dave,
You can also turn this into a shortcode to display it in a post or a widget quite easily.
The code below will simply output the number of users (without the word “users” after it) when the shortcode [NUMBERUSER] is used.
add_shortcode(‘NUMBERUSER’,’show_number_users’);
function show_number_users(){
$total_users = count_users();
echo $total_users[‘total_users’];
}
Hope this helps someone out!
All the best,
Manny.
Dave Warfel (Author) / #
Great idea, Manny. I can see how a shortcode might be helpful here. Thanks for sharing.
Michael / #
I’m sorry, but I’m not real WordPress literate – could you tell me exactly WHERE I should put the code to display users? Thanks!
Dave Warfel (Author) / #
Michael,
You have a few options as to where you place the code.
If you use the original code I posted, you would need to place that in one of your theme’s template files. For example, to place it on all single posts, you’d put it in single.php. To place it in the sidebar somewhere, you would put it in sidebar.php. These files are found inside of your current theme folder.
If you wanted to output the number of users inside the actual content of a post or page, you should look at Manny’s advice above. You would place Manny’s code in your theme’s functions.php file, and then use “[NUMBERUSER]” inside of an actual post or page.
Does that make sense?
Michael / #
Yes, that helps. I would have to put it in my theme, as I am not experienced with PHP coding, and wouldn’t want to screw up my site! LOL Thanks very much.
Dave Warfel (Author) / #
Haha, understood. It is pretty basic PHP code, but you always want to be careful when editing a functions.php file. Best of luck.
Trevor Townsend / #
I know this is an older thread, but I just found it and tried it – I could not get the code to work as described on WP 4.5.3
Dave Warfel (Author) / #
Trevor — If you post a code sample, I’ll take a look and see if I can help.
You can use something like codepen or jsfiddle to post your code.