WP-ShortStat Slowing Down Root Index Pages

For over a year now, I have been using Markus Kämmerer’s (Happy Arts Blog) WP-ShortStat plugin for WordPress. The plugin is relatively well-maintained and remains one of my favorite admin tools. Great for popping in on stats without logging into Mint. Nonetheless, due to its IP/country-detection functionality, WP-ShortStat has experienced its share of difficulties (e.g., read through the change log on the plugin’s home page). In this article, I describe how WP-Shortstat slows down the root index-page of a site, and then suggest a (temporary) fix for the issue.

The Issue

I have heard of several different WP-ShortStat problems resulting from the country-lookup feature, including missing statistics, broken pages, and pages not loading (due to slowness of lookup server, etc.). Too many problems for something I really never look at anyway. The new (for me, anyway) issue with WP-ShortStat involves an extremely slow (or even non-loading) root index-page of any site running WordPress installed in a subdirectory. I experienced this problem under the following conditions at the exact same time and on three different blogs:

  1. WordPress installed in a subdirectory (not root)
  2. Running Markus Kämmerer’s WP-ShortStat plugin (version 1.7h)

The problem is that, all of sudden, and for no apparent reason (i.e., no changes had been made), the index.php files located in the root directory (i.e., outside of the WordPress installation directory) stopped loading. Well, okay, they would load, if you let them spin for long enough (average load time: ~3.33 minutes!), which is the same as not loading at all given the average visitor’s attention span. The bizarre thing is that every other page on the sites loaded normally (ha!). Indeed, all pages within the blog itself continued to load quick, fast, and in a hurry, but for some reason, the root index.php page would not. Here is a summary of the symptoms:

  1. Suddenly, the site’s root index.php page will not load (or will load very slowly)
  2. Meanwhile, all other site pages continue to load normally and without problems

The Solution

Granted, most of my traffic is routed directly to internal pages (thankfully, only a fraction of total views are for the root page), so it is doubtful that many visitors noticed the problem before I could solve the issue. At first, I had NO idea why the root page — and ONLY the root page — would not load. Total stumper. Eventually, after a few rounds of the plugin-elimination game, the culprit was revealed! Disabling the WP-ShortStat plugin completely resolved the issue.

And that was just fine as a temporary fix, but I wanted to continue using the WP-ShortStat plugin — I just wasn’t ready to say goodbye (sniff). So, after some research1, trial and error, it became clear that the offending aspect of the plugin involved that incredibly uninteresting IP/country-lookup feature. Nice — I never used it anyway!

Thus, I decided to hack the plugin, solve the problem, and keep WP-ShortStat running tuf. Fortunately, the fix is a snap. Crack open the plugin file, scroll down to around line #21, and locate the following:

function determineCountry($ip) {
		
		$coinfo = @file('http://whois.happyarts.net/ip2country.php?ip=' . $ip);
		$country = trim($coinfo[0]);
        
		if($country == '(Private Address) (XX)' 
		|| $country == '(Unknown Country?) (XX)'
		|| $country == '' 
		|| !$country 
		  )return 'Indeterminable';
			
		return $country;

}

Now, simply comment out the first two variable declarations, $coinfo and $country like so:

function determineCountry($ip) {
		
		// $coinfo = @file('http://whois.happyarts.net/ip2country.php?ip=' . $ip);
		// $country = trim($coinfo[0]);
        
		if($country == '(Private Address) (XX)' 
		|| $country == '(Unknown Country?) (XX)'
		|| $country == '' 
		|| !$country 
		  )return 'Indeterminable';
			
		return $country;

}

..and that should do it. Upload, test, and enjoy. If you were experiencing the issue described above — or any similar issue (pages not loading, slow server response, etc.) — this fix should definitely solve the problem. Sure, you won’t enjoy those nifty country IP lookup data, but you will eliminate all third-party server issues and other related problems.

If this hack worked for you, I would love to hear about it. Or, if you have encountered similar problems, or found a different solution to this or a related issue, please leave a comment and share your wisdom!

References