An Introduction to the User Stats Module for Drupal

The User Stats module inserts information on a user into their posts on a site. It is aimed at forum administrators who need to see the post counts, date registered and IP addresses of their users. This article provides an introduction to the User Stats module and shows how it may be used in themes.

Important Note for Advanced Forum Users

When using Advanced Forum, no theme work is required for User Stats. Just enable the User Stats module, then refer to the Advanced Forum documentation.

What you need to know

Before installing User stats it is helpful to understand the basics of creating Drupal themes. The documentation on theme authoring is comprehensive, it’s not necessary to understand everything though, use it as a reference. The parts of the documentation most relevant to this article are:

  1. Theming nodes by content type
  2. node.tpl.php

Installation and Configuration

To install the module upload it to your server; usually the best place to put modules is the sites/all/modules directory (if it doesn’t exist, create it). Then, in the Drupal interface, click Administer -> Site building -> Modules. From there check the User stats check box and click Submit. This will install the module.

To configure User stats select Administer -> Site configuration -> User stats from there it is possible to control what, and how, metrics are counted. When starting out it’s safe to leave settings at their defaults.

Adding Stats to a Forum Theme

Firstly, make a copy of node.tpl.php in your theme directory, name it node-forum.tpl.php Don’t worry if this file is already in the theme directory, leave the existing file and carry on to the next step. Although these instructions concentrate on forums, it is possible to add statistics to any node type.

Open node-forum.tpl.php file, in your favourite HTML editor, and add the following to it:

Post count: <?php print user_stats_get_stats('post_count', $node->uid); ?>

The post_count is the name of the statistic to show, a complete list of names is below.

  • ip_address — IP Address of the user that posted the node;
  • join_date — time stamp representing when the user joined;
  • login_count — number of times the user has logged-in;
  • login_days — number of days since the user was last logged-in to the site;
  • post_count — number of nodes and comments created by the user (the type of posts to count is configurable in the User stats settings page);
  • post_days — number of days since the user last posted;
  • reg_days — number of days since the user registered on the site;
  • online — whether the user is online (basically whether they have done anything in the last 15 minutes);
  • profile — returns a user profile field (from the Profile module);

Most of the results from these can be printed straight into a template (showed in the example above), a couple are trickier.

Join Date

The join_date option returns a time stamp, which looks like 1166294536. This is not very readable, so we run it through the format_date function:

Join Date: <?php print format_date(user_stats_get_stats('join_date', $node->uid), 'custom', 'd/m/Y'); ?>

Online/Offline Indicator

Using this option won’t return anything printable, so this code can be used instead:

Online: <?php print (user_stats_get_stats('online', $node->uid) ? 'yes' : 'no'); ?>

Or, for a theme with online and offline icons, this can be used to print the corresponding one:

<img alt="User is <?php print (user_stats_get_stats('online', $node->uid) ? 'online' : 'offline'); ?>" src="<?php print base_path() . path_to_theme() . '/' . (user_stats_get_stats('online', $node->uid) ? 'online.png' : 'offline.png'); ?>" />

Adding Statistics to Comments

Comments have a separate template to nodes (the main body of a page). The process for adding statistics to comments is the same as shown above, with two important differences:

  1. the template file to edit is comment.tpl.php ;
  2. instead of $node->uid use $comment->uid ;

As an example, this is the code to get the post count in a comment:

Post count: <?php print user_stats_get_stats('post_count', $comment->uid); ?>

Hiding Stats of Anonymous Users

User stats are stored for anonymous users too, as it may be desirable to not show these, this is how to suppress them:

<?php if ($node->uid <> 0): ?>
Post count: <?php print user_stats_get_stats('post_count', $node->uid); ?>
<?php endif; ?>

Clever Post Count Example

The post count example below uses Drupal’s clever format_plural() function, to show the plural ‘posts’:

User has <?php print format_plural(user_stats_get_stats('post_count', $node->uid), '1 post', '@count posts'); ?>

Integration with Other Modules


User stats integrates with the Token module. This allows various modules to integrate with User stats.

Token is not of much interest to the average theme designer or site admin.


Workflow-ng allows a site admin to setup events, rules and actions. When an event happens Workflow-ng checks the rules, added by the site admin. If the rules are passed then Workflow-ng takes an action.

For example: when a user creates a new post, they now have a post count higher than three and have been a member for more than seven days, they should be given the role: Member level one.

The user creating a new post is the Workflow-ng event. Checking whether they have over three posts and have been a member for longer than seven days are the rules. The action is giving them the role of Member level one.

This integration is how The Webmaster Forums control access to their Market Place Forums


The reader should now be able to add User statistics to their site. Questions, comments and suggestions for improving this documentation are welcome on our Webmaster forum.

Please note: this article will be changed as new versions of User stats are released.

About the Author

Liam McDermott is the technical bod at The Webmaster Forums. He also writes articles and loves dallying with Drupal. His business site is InterMedia.