Daily Archives: September 9, 2009

Simple Stats – Visitors Online

This post is designed to accompany Chapter 3 of my book, the WordPress-MU 2.7 Beginner’s Guide.

Would you like to show some simple stats in the sidebar of your WordPress-MU site?  Something like the stats in the screenshot below?

Visitor Stats

Visitor Stats

WordPress-MU makes it easy to pull information about the number of blogs and users that your site has, and display that information on the page, just use something similar to the following code.

<li id="Stats">
<h2>Site Stats</h2>
$stats = get_sitestats();
echo "MYSITE.com currently has
<b>".$stats[ 'blogs' ]."
</b> blogs and
<b>".$stats[ 'users' ]."
</b> users."; ?></li>

The above code will display the number of blogs that have been registered, and the number of user accounts that have been created.

If you’d like to show the number of visitors currently online, there are a number of ways you can do this.  The easiest is to use a simple visitor tracking script that will keep a count of recently active sessions.

Download this Simple visitor tracking script (right click and save as, then re-name the file to vonline.php).  This is a slightly edited version of a script that I’ve been using on my sites for a couple of years.  I did not write it, if anyone recognizes it, please let me know in the comments so I can give credit to the original author.

Upload the vonline.php file to your site (for simplicity, let’s pretend you put it in the same folder as your sidebar.php file – if you put it somewhere more central, then you will need to change the path used in the include statement to reflect that).

Then add the following line to the code listed above (place it just before the </ul> tag).

<li><?php include('vonline.php') ?></li>

Congratulations, now you have some visitor stats on your WordPress-MU blog home page!