25 February 2005

WordPress IP to Country Plugin

Posted under: at 00:30

Wordpress IP to Country plugin is adapted from IP-to-Nation plugin for Wordpress. The only difference is that my IP to Country plugin uses (surprise!) the IP to Country database instead of IP to Nation database. There is also the GeoIP plugin which uses the GeoIP database.

I’ve been using the IP to Nation plugin, but I feel that the database is not good enough. An IP from Germany, for example, sometimes get identified as from Europe. My previous experience with the GeoIP database was much better, but the plugin itself seems to be unmaintained. It was not hard to adapt the IP-to-Nation plugin to use IP to Country database since both uses database backend and uses similar algorithm. Thanks to Ozh for creating IP to Nation plugin in the first place.

Requirement

Any version of WordPress above 1.2 should suffice. Some basic knowledge of PHP and editing WordPress template are also required.

Installation

Next, the installation instruction. The installation is a bit more involved than IP to Nation since IP to Country only provides its data as a CSV file, not as SQL dump file.

  1. Download the IP to Country database
  2. Unzip it to get a CSV file.
  3. Create the table ‘iptocountry’ to store the database: CREATE TABLE iptocountry (ip_from int(4) unsigned, ip_to int(4) unsigned, country_code2 char(2), country_code3 char(3), country_name varchar(50));
  4. Create the cache table ‘iptocountry_cache’ if you wish to use caching. Caching is recommended for busy sites, but for smaller blogs it can unnecessarily waste space: CREATE TABLE iptocountry_cache (ip int unsigned, country_code2 char(2), country_code3 char(3), country_name varchar(50));
  5. Populate the table: load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\r\n';. Note: if you uploaded the CSV file using FTP in ASCII mode, you need to do this query instead: load data infile '/path/to/ip-to-country.csv' into table iptocountry fields terminated by ',' enclosed by '"' lines terminated by '\n';. You might not be able to do this if your database user lacks privilege, in that case, you will need to write a CSV parser to do this, should be easy enough if you have some programming skill. If you wrote one, please share it so others can benefit. Note: Praveen has made available an SQL dump of IP to Country data. You can use that if you have difficulties loading CSV file.
  6. Download the plugin, rename it into wp_ip2country.php and put it in your wp-content/plugins directory.
  7. If you wish to use caching, please enable it near the top of the wp_ip2country.php file.
  8. Activate the plugin from your Wordpress plugin menu.
  9. (optional) Get flag images. Ozh provides a download link here, or you can also download another version from GeoIP.
  10. Modify your templates as you see fit

Available functions

  • wp_ip2c_getCountryName($display, $ip): prints/returns country name, e.g. Indonesia
  • wp_ip2c_getCountryCode2($display, $ip): prints/returns two letter country code, e.g. id
  • wp_ip2c_getCountryCode3($display, $ip): prints/returns three letter country code, e.g. idn

The IP to Country database uses CAPITAL LETTERS exclusively. But with this plugin, the country name is always printed/returned with capital first letter in each word, and the country code is always printed/returned in lower case.

If $display is set to 1, the function will print the output. If $display is set to 0, the function will return the output so that you can use the returned values. $ip can be omitted, this way the function will use cached data from previous invocation. You can use this feature to minimize database lookups. If there is no cached data and $ip is not provided, the function will use the server variable $REMOTE_ADDR as the IP address to check.

Examples

This will display visitor’s country flag and name.

You are from
<img src="flag-<?wp_ip2c_getCountryCode2(1)?/>.gif" />
<?wp_ip2c_getCountryName(1)?>

Put the following inside comment loop for displaying commenter country name and flag.

Posted from
<img alt="<?php wp_ip2c_getCountryName(1,$comment->comment_author_IP); ?>"
src="/flag/flag-< ?php wp_ip2c_getCountryCode2(1); ?>.gif"/>
< ?php wp_ip2c_getCountryName(1); ?>

Note

IP to Country database is not normalized unlike IP to Nation. So, it takes a bit more space. In my case, IP to Country takes about 1.58 MB of space, and IP to Nation takes about 350 KB.

Demonstration

You are coming from flag United States.

I hope this will deter people from making comment tests below :P

Changelog

2.0: Implements caching, useful for busy sites.

230 Responses

Trackback: Use this URI to trackback this entry. Use your web browser's function to copy it to your blog posting.

Comment RSS: You can track conversation in this page by using this page's Comments RSS (XML)

Gravatar: You can have a picture next to each of your comments by getting a Gravatar.

Karma Threshold: none -3 -2 -1 0 +1 +2 +3