0

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 ?

0

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' );
| improve this answer | |
  • The return value of a shortcode handler function is inserted into the post content output in place of the shortcode macro. Remember to use return and not echo - anything that is echoed will be output to the browser, but it won't appear in the correct place on the page. codex.wordpress.org/Shortcode_API#Output – Antti Koskinen Jul 25 at 9:07
  • count_users($strategy, $site_id) documentation, developer.wordpress.org/reference/functions/count_users, notes that counting can be either CPU- or memory-intensive depending on chosen counting strategy. If the site has only few users and very low traffic, then this might not be an issue. With a bigger userbase and busier site caching the count result into a transient, and not counting users again on every single pageload, can help save some resources. – Antti Koskinen Jul 25 at 9:15
  • Thank you for your time to answer, but this will show all roles and i want 2 role only like i mentioned above. If you can tell me to put 2 roles only with total count user of them and i can rename the 2 roles with another text. I hope you understand what i mean. – Adnan Omar Jul 25 at 11:17
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.