WP-ShortStat plugin broken after upgrading to WordPress 2.0.2

The visitor statistics plugin Wp-Shortstat stops logging new hits after a WordPress uprade to 2.0.2 (It still works in version WordPress 2.0.0, haven’t checked 2.0.1).  Here’s a fixed version as .txt file (save on your harddisk and omit the .txt).  Some info on the package and the bug:

On WP-ShortStat

WP ShortStat is the easiest stats package for WordPress blogs you can imagine.  Simply download the file, upload it to your plugins directory, activate it in the WordPress Control panel, and you’re done.  Two extra tables are created by the plugin on activation.  It is based on Shortstat, a predecessor of what’s now been known as Mint

It’s a very quick solution if you need to monitor all behaviour on your website (searchbots, feedtraffic and browsers).  I would not recommend it for other than private and short-term use however…  If you want really meaningful information on human visitors, you’ll have to switch to a webbug-based system (see these postings on referer spam and phpopentracker).  And because all site requests are logged in Mysql, you get huge tables and performance problems in no time (PhpOpentracker has the same problem, BTW).  But then again, simply emptying the tables from time to time probably is less trouble than other more complex self-hosted solutions!

On WordPress plugins

Plugins are php functions you can add to your WordPress install that modify output (“filters”) or to change behaviour (“actions”).  With the Plugin API you need to “hook” your own filters and actions into the WordPress flow, using the function: add_action (<your_function>,<wordpress_function>) Have a look at this 5–minute video tutorial on writing a basic plugin.  As an example:  I wanted my SiteDeals15 Released page to appear as a posting on both the blog and in the RSS feed (normally pages don’t show up in either of them).  So I wrote a  plugin to change both the title and the link of my post on Digg RSS feeds, and have it point to the page. Have a look at the plugin (as txt file): it just contains two functions and  “hooks”. 

How to chase a WordPress plugin bug

Find out in which “hook” the problem is.  In the WP-ShortStat, a text search for “add_action” shows there are 4 hooks:

wp-content\plugins\wp-shortstat.php(747): add_action('shutdown', array(&$wpss, 'track'));
wp-content\plugins\wp-shortstat.php(751): add_action('init', array(&$wpss, 'setup'));
wp-content\plugins\wp-shortstat.php(838): add_action('admin_menu', 'wp_shortstat_add_pages');
wp-content\plugins\wp-shortstat.php(974): add_action('admin_head', 'wp_shortstat_css');

Judging from the names, the most likely candidate is the “track” function that is called on shutdown: before finishing every blog page, WP-ShortStat writes a log entry to the database.  The “track” function starts with this condition:

if($wpdb->is_admin
  || strstr($_SERVER['PHP_SELF'], 'wp-admin/')
  || is_404()
  || is_admin_page()
    )return; // let's not track the admin pages -- no one cares.

 If you play a bit with the code, it turns out that “is_admin_page()” is always true in WordPress 2.0.2!  That has to do with the definition of this function:

if( !function_exists('is_admin_page') ) {
 function is_admin_page() {
  if (function_exists('check_admin_referer')) {
   return true;
  }
  else {
   return false;
  }
 }
}

So here’s the problem: one of the ways to check whether the visitor is in the control panel and should not be logged, is a check whether the function ‘check_admin_referer’ is defined.  Turns out that, in WordPress 2.0.0, this function has moved from the file “wp-admin\admin-functions.php” (only included for admin pages)  to a file “\wp-includes\pluggable-functions.php” which is always included, so ShortStat thinks of every page as an admin page, and doesn’t log any request anymore.  I don’t know enough about Wordpress to find out there are more elegant ways to do so, but this particular bug is quickly fixed by replacing the  ’check_admin_referer’  condition by a check on “write_post”, which is still in the “wp-admin\admin-functions.php”.

And if you have read this far, you should have no more fear to write your own WordPress plugin and write something more meaningful than I just did :-)  

Update March 26d

I learnt via this trackback the problem had been solved before by the people from HappyArts.de (see the "older changes" at their WP-ShortStat project page). Their release also includes other bugfixes and localised versions of the plugin. Should have googled better before I started :-).

16 Responses to “WP-ShortStat plugin broken after upgrading to WordPress 2.0.2”

  1. WP-ShortStat Plugin with Wordress 2.0.2 Upgrade | iBlogMarketing.com Says:

    [...] After successfully upgrading form WordPress 2.0 to 2.0.2, I no longer received Web stats from WP-ShortStat, an excellent free statistics plugin for WordPress.  I discovered hot fix a fix at Pascal’s website.  Just download the text file, save as php file, upload to your plugin folder on your Web server, and you should be ready to go again.  Thanks Pascal for the fix.  I owe you one!Technorati Tags: wp-shortstat, wordpress, plugins Filed under WordPress by Christian Del Monte. Permalink • Print • Email [...]

  2. » Because I Write Says:

    [...] PS: It’s now done, nice and easy. I have also activated the wp-shortstat plugin recently hacked for 2.0.2 by Pascal. I’ll see if all my required plugins are working fine before I upgrade this site to the latest version. 2.0, upgrade, WordPress, WP Plugins [...]

  3. The LOdown » Blog Archive » Finding a ShortStat Fix For WordPress 2.02 Says:

    [...] After seeing the new stats tracker used at WordPress.com, I realized that it would be nice to have something similar on my hosted copy of WordPress. I found this great looking ShortStat plugin, but was frustrated that neither the copy on the WordPress plugin Wiki nor a few attempted fix copies (like this one, and this one) worked properly on my install of WP 2.02. [...]

  4. Afvragingen Says:

    Bloggersbijeenkomst in Antwerpen 21 april…

    Bloggers komen af en toe samen. Zo ging ik bijvoorbeeld zelf naar een ribbekes-avond in Gent en een poepsjieke spaghetti-conference in Brussel. Op die laatste meeting hadden Jeroen en ik hier en daar aangekondigd dat we een volgende meeting in……

  5. chilled.hu » shortstat wordpress-hez Says:

    [...] melóhelyen shortstatot is használom wp-hez és egy ideje nem számolt a dög… ma már igen idegesített ezért nekiláttam keresni és megtaláltam a hiba javítását ezen az oldalon. [...]

  6. LLam Says:

    thnx!

  7. ihad Says:

    the happyart.de blogs seems down. I am looking for an alternate source for the fixed stats file. cheers.

  8. Michael Huynh Says:

    Hey Pascal,

    I was having the same problem with WP 2.0.2 breaking my Simple Asides plugin. I also traced it down to the is_admin function, but upon googling it, I found your page and a simple fix to that problem. Thanks for your help!

    Michael Huynh

  9. Depechie Says:

    Maybe check out PMetrics ( http://performancing.com/metrics/start ) !
    Works great with WordPress… and gives very detailed information about users visiting your blog.

    Just a thought :)

  10. Pascal Says:

    Am using it already (check the html source) :-)
    I’m just slow at blogging…

  11. OSS AtoZ » 在WordPress中2.0.2中安装WP-ShortStat插件 Says:

    [...] WP-ShortStat plugin broken after upgrading to WordPress 2.0.2 [...]

  12. Mateus Neves Says:

    Fatal error: Cannot access empty property in /home/.gerard/mateusneves/thewpthemes.com/wp-content/plugins/wp-shortstat.php on line 779

    Solution?

  13. Mystique Says:

    I have 2.03 here.. and well.. all I get is this:

    ShortStat

    Hits Uniques
    Since

    in a little box on the upper left of area..

    in the html this is all I get..

    ShortStat

    Hits Uniques

    HitsUniques
    Since

    then it ends..

  14. Robin Says:

    Strange, I have 2.0.4 and it works fine for me without any modification! :)

  15. Andrew Ferrier’s Blog » Blog Archive » Fix for Popularity Contest Page Views on Wordpress >2.0.0 Says:

    [...] I installed the popularity contest plugin some time ago to count the popularity of posts on this blog. Unfortunately, up till now it hadn’t been counting page views, which significantly skewed the statistics. I recently discovered, however, that there was a bug in the WP-ShortStat plugin which caused it not to register hits (due to a regression in WordPress). I applied a similar fix to popularity-contest.php version 1.1 and it now works. It looks like a different fix has already been applied to this version in Trac to fix the problem. [...]

  16. Wordpress: shortstats plugin not working Says:

    [...] Solution: The problem was found and fixed on this blog AND it was fixed on this project site. In either case, the solution involves downloading an updated [...]