GeoIP for Wordpress Counterize Plugin
This is a bit of an esoteric subject, but if you use Wordpress and are looking for a good statistics plugin, try Counterize II. If you’re looking for an even better statistics plugin, keep reading.
Steffen Forkmann’s Counterize II plugin for Wordpress is pretty good, but as of 2.13, it stopped logging IPs. For this reason, I’ve been using 2.12 for some time. Tonight, I decided to take the statistics display a bit further by installing a geographical lookup component, and adding an ip to hostname lookup. My version displays the visitor’s city, region, country, and hostname instead of just their IP - or worse yet - nothing. Note: As Steffen has forewarned, it is illegal to log IPs in Germany, so don’t break the law if you live under such (illiberal) legislation.
The modifications involve using a GeoIP database from Maxmind. There are a number of different API’s and databases available - I have chosen to work with the PHP PEAR API and the (Lite) City database. (They want you to pay big bucks for the non-lite non-free version.)
If you haven’t already done so, install PEAR, and GeoIP. On a Red
Hat (Fedora) system such as my server, this means:
bash# yum install php-pear GeoIP
Next, install the Net_GeoIP package for PHP/PEAR.
bash# pear install Net_GeoIP-1.0.0RC1
Finally, download the GeoLiteCity database into the GeoIP shared
content folder:
bash# cd /usr/share/GeoIP
bash# wget
http://www.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
bash# gunzip GeoLiteCity.dat.gz
At this point, you should be able to download and install Counterize
II 2.12.7, and then install my patch to the main script. To do
this, go to the counterizeii folder in the plugins directory:
bash# cd
/path/to/wordpress/wp-content/plugins/counterizeii/
Download my
patch:
bash# wget
http://www.jamesonwilliams.com/bin/counterizeii-upgrade.patch
And patch the counterizeii.php file:
patch < counterizeii-upgrade.patch
That should do ‘er. I haven’t actually tried to apply that patch, so the patch command might need tweaking. Let me know if you have any problems getting this going or using it.
Note: A similar result can be achieved by reading the file contents of a remote url that performs such lookups, but you will find that this is quite slow when done in a 300-item long for loop.