How to display total user count by specific role in WordPress as statistics?

i want to display in my Home page total user count by specific role in WordPress as statistics

I already created Role (subscriber, contributor) and i want to show the total users for each of those roles.

Can You Please point me also with the answer where i can put the code ?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

insert this code in your theme functions.php and use this shortcode [users_count_bro] to display where you want

function total_count_bro() {
    $out             = '';   
    $user_count_data = count_users();
    $avail_roles     = $user_count_data['avail_roles'];
    foreach ( $avail_roles as $role_key => $role_count ) {
        $out .= $role_key.':'.$role_count.'<br/>';
    }
    return $out;
}
add_shortcode( 'users_count_bro', 'total_count_bro' );

Method 2

I manage to do it like this:

/* user count */
// Function to return user count 1
function total_count_bro() {
    $out             = '';   
    $user_count_data = count_users();
$avail_roles = $user_count_data['avail_roles'];
foreach ( $avail_roles as $role_key => $role_count ) {
        $out = $contributor = $avail_roles['contributor']; /* User role author  */'<br/>';
    }
    return $out;
    }
add_shortcode( 'users_count_bro', 'total_count_bro' );

// Function to return user count 2
function total_count_bro2() {
    $out             = '';   
    $user_count_data = count_users();
$avail_roles = $user_count_data['avail_roles'];
foreach ( $avail_roles as $role_key => $role_count ) {
        $out = $subscriber = $avail_roles['subscriber']; /* User role administrator */'<br/>';
    }
    return $out;
    }
add_shortcode( 'users_count_bro2', 'total_count_bro2' );

This will create the total count for the user by role and it will show the number count only.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x