Add Users Total To WordPress Dashboard

By XPS on January 21, 2012.     What do you think of this article?

The WordPress Dashboard is the first thing you see when you login. The “Right Now” box has important information like the total number of posts tags, comments, etc. but one thing is missing: users. I usually don’t have a need to check the Users list, so when a number of SPAM accounts registered, I didn’t even realize it. It got me thinking about writing a script to display the total number of registered users right there in the Dashboard.

During my research, I came across another site that had the code already done. Score! What you’ll want to do is add the following code to the bottom of your theme’s functions.php file:

/**
* Add users total to "Right Now" dashboard widget
*/
if ( !function_exists('wps_dashboard_user_count') ) {
  function wps_dashboard_user_count() {
    global $wpdb;
    $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users");
    echo '<table><tbody>
      <tr class="first">
        <td class="first b b_users"><a href="users.php">' . $users . '</a></td>
        <td class="t users"><a href="users.php">Users</a></td>
      </tr>
    </tbody></table>';
  }
  add_action( 'right_now_content_table_end', 'wps_dashboard_user_count');
}

Credit of this script goes to Kevin Chard of WPSnipp.com who has a large collection of WordPress scripts I plan to further browse.

My script above has minute adjustments to his work.

  • First is the comment section at the top. Keeping comments in your source code helps organize code projects and remember what they do when you review them later on.
  • Second was to encase Kevin’s code within a function that checks whether a function of the same name was already defined. Most snippet creators amend the function’s title with an abbreviation of their site name, like this one did with “wps_“. Although unlikely, it’s still possible a function with the same name already exists, so having a function to check will prevent issues.
  • The other minute changes included keeping the same coding style as the WordPress developers by referring to members as Users and doing the same within the table cells CSS class identifiers.

Like I said, pretty small changes to an already good script.

You will notice that the column which says “Users” isn’t aligned to the others like Tags, Posts, etc. That’s because the action right_now_content_table_end adds a table below the already existing table. I don’t know of an action to add this function to the already existing table, but you can use different actions to move this total count around the “Right Now” box. Try replacing the “action” line with add_action( 'right_now_content_table_end', 'wps_dashboard_user_count'); or add_action( 'right_now_content_table_end', 'wps_dashboard_user_count'); to see which spot you like best.

Posted in: Web Development



XPS is the site owner of Computer Tech Tips and is passionate about computer technology, particularly Windows-based software, malware removal, and web development. He enjoys helping people troubleshoot computer problems and providing technical support to family, friends, and people around the net. XPS wrote 62 article(s) for Computer Tech Tips.


What do you think?

Your email will not be published or shared, but is required to aid SPAM prevention. The math question also helps to avoid SPAM and must be correctly answered. If you want your comment accepted, don't use keywords or your website as your name.

(required)

(optional)

Note: Comments need to be approved before shown.

What is 6 + 4 ? (required)
Please leave these two fields as-is:

Notify me of followup comments via email.
Click here to subscribe without commenting.