Add Snippet To Project
Do you want to check the total number of members on your WordPress site? Whether you manage a multisite network or a simple blog, keeping track of your user base is essential. This is especially relevant for community-based websites or membership sites where user engagement is a key metric. In this article, we’ll teach you how to easily find and display the total number of registered members on your WordPress platform.
function wpturbo_total_number_of_members() {
$result = count_users();
return $result['total_users'];
}
echo 'Total members: ' . wpturbo_total_number_of_members();
This code snippet defines a function named wpturbo_total_number_of_members()
that utilizes the WordPress built-in function count_users()
to obtain information about the user database.
Let’s dissect it.
function wpturbo_total_number_of_members() {
$result = count_users();
return $result['total_users'];
}
Here, the count_users()
function is called within our custom function. The count_users()
function is a built-in WordPress function that retrieves and returns an array of information about users registered to the site, including the total number of users and the number of users per role.
Next, we store this array into the $result
variable. In the array returned by count_users()
, the total number of users is stored with the key 'total_users'
. Therefore, to get the total number of users, we access this value from the $result
variable by specifying the key as $result['total_users']
.
Finally, in our custom function, we return this value. Essentially, when we call the function wpturbo_total_number_of_members()
, it returns the total number of registered users on the WordPress site.
echo 'Total members: ' . wpturbo_total_number_of_members();
In this part of the code, we output a string that includes the total number of users. The .
operator is used to concatenate (or join) the ‘Total members: ‘ string and the return value of wpturbo_total_number_of_members()
function. This outputs something like "Total members: 25" (with 25 being the number of registered users on the site).