Add Users Total To WordPress Dashboard

By .     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
 * Taken from https://www.computertechtips.net/108/add-users-total-to-wordpress-dashboard/
 */
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 last 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 existing table with rows from Posts through Tags is already closed, and the action right_now_content_table_end adds content after that table closed. As far as I know, their isn’t an action built into WordPress to add content within the existing table. The reason the script I provided creates a new table is to take advantage of the existing style attributes.

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_table_end', 'wps_dashboard_user_count');
to move it below the SPAM count. This page of the WP Codex lists more “Right Now” widget actions.

Posted in: Web Development



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 78 article(s) for Computer Tech Tips.


 Subscribe to comments: this article | all articles
Comments are automatically closed after a period of time to prevent SPAM.