How to show the total number of registered users on your WordPress blog by adding a function to your website’s functions.php file.
This method is slightly more advanced but can be a useful option if you want to display the total number of registered users in a more customized way. In this blog post, we will walk you through the steps to show the total number of registered users of your WordPress blog using the functions.php file.
Here are steps:
- Access your WordPress Dashboard
- Access the functions.php file
- Add the function
- Display the total number of registered users
- Customize the display of the total number of registered users
Step 1: Access your WordPress Dashboard
The first thing you need to do is access your WordPress dashboard. You can do this by logging in to your WordPress website and clicking on the “Dashboard” button located on the top left-hand side of the screen.
Step 2: Access the functions.php file
Once you are in the WordPress dashboard, navigate to the “Appearance” section located on the left-hand side of the screen. Click on the “Theme Editor” button to access your website’s theme files.
In the Theme Editor, you will see a list of files located on the right-hand side of the screen. Click on the “functions.php” file to access it.
Step 3: Add the function
To show the total number of registered users, you will need to add a function to the functions.php file. To do this, add the following code to the file:
function total_users() { $users = count_users(); return $users['total_users']; }
This code will create a function called “total_users” that will count the total number of registered users on your website.
Step 4: Display the total number of registered users
Once you have added the function, you can display the total number of registered users on your website by adding the following shortcode to any page or post where you want to display the total number of registered users:
<?php echo total_users(); ?>
This shortcode will automatically display the total number of registered users on your website.
Step 5: Customize the display of the total number of registered users
If you want to customize the display of the total number of registered users, you can use HTML and CSS to style the shortcode. For example, you can change the font size, color, and alignment of the shortcode to match your website’s design.
Here is another way using $wpdb
Have you enabled user registration on your WordPress site? Does your blog accepts Community Members Registration? Then below short code will help you show Total Number of Registered Users of your WordPress Blog.
Put below code anywhere in your theme’s file i.e. Single.php, Header.php, index.php, etc where you want to show your user count.
WordPress provides a global variable, $wpdb, which is an instantiation of the class already set up to talk to the WordPress database. Always use the global $wpdb
variable. (Remember to globalize $wpdb before using it in any custom functions.)
<?php global $wpdb; $user_count = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->users" ); echo "<p>User count is {$user_count}</p>"; ?>
Simply save the file, clear the cache and reload page. You should see total # of users on that page.
Do let me know if you see any problem with this.