WordPress IP to Country Plugin
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.
- Download the IP to Country database
- Unzip it to get a CSV file.
- 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)); - 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)); - 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. - Download the plugin, rename it into wp_ip2country.php and put it in your wp-content/plugins directory.
- If you wish to use caching, please enable it near the top of the wp_ip2country.php file.
- Activate the plugin from your Wordpress plugin menu.
- (optional) Get flag images. Ozh provides a download link here, or you can also download another version from GeoIP.
- Modify your templates as you see fit
Available functions
wp_ip2c_getCountryName($display, $ip): prints/returns country name, e.g. Indonesiawp_ip2c_getCountryCode2($display, $ip): prints/returns two letter country code, e.g. idwp_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
United States.
I hope this will deter people from making comment tests below ![]()
Changelog
2.0: Implements caching, useful for busy sites.
Hello, nice to see some other plugin on the same topic
As for IP databases, I think each one can be better or worse than the other one depending on the IP. You say for example that ip2nation sometimes identifies Germany as “Europe”, I see that IP-to-Country identifies me as, on their website, “You are from United States and your IP Address is unknown.” which I think is kind of pathetic
(I’m from France, and why the hell would my IP be unknown ?)
It’ll be interesting nonetheless to see what feedback you’ll have from visitors and users from all over the world regarding accuracy of the database you’re using, I’m looking forward to see how it turns out
Again, cheers !
weird. It displays here the correct flag, but they just cannot identify me on their own website
That’s probably simply a bug on their web site and their code choose United States by default
. I don’t know how accurate IP to Country is, this is my first time using it. I guess we’ll see how it goes.
BTW, thanks for IP to Nation plugin…
Now, we’ll see if the flag is right… Anyway, nice plugin tho.
[…] IP to Country Thursday February 24th 2005, 5:17 pm Filed under WordPress Plugins WordPress IP to Country Plugin is adapted from IP-to-Nation plugin for Wor […]
So? where I come from? with this new Plug in?
another plugin test.
Dari Samarinda Flag nya apa ya …?
wah wah
boleh ngejunk flag di sini ya?
Well, this is a interesting plugin…
I like it…
[…] endungen dokumentiert
WordPress IP to Country Plugin
Das WordPress IP to Country Plugin setzt eine IP in ein Land um und liefert de […]
Test from Sydney, Australia
Test for the UK…
Testing and greetings from Singapore!!!
Make sure you use the GeoIP Flags and not the IP2Nation ones as some of the flags have different names… For example the GB flag for IP2Country is called flag_uk.gif in the IP2Nation collection, likewise there are issues for the European Flag…
Cool plugin ^^
Test from Taiwan!!
testing India
#15: I happen to use both. I unzipped GeoIP flags, then replaced them with IP2Nation. So I have both gb.gif and uk.gif.
FYI, the IP2Nation version has about one dozen more flags than the GeoIP one. Some flags in GeoIP are in different size than the others. In IP2Nation, only one flag (Syria) is in different size than the others. So, I think this is an indication that the IP2Nation version is newer than GeoIP.
Just testing to see whether this resolves my IP more accurately than IP2Nation (which thinks I’m in the US) though I’m actually in Canada.
test, ini dari jakarta
hello from turkey
Another test for Sydney Australia.
I wonder if I can use this plugin to identify which countries I blog from, giving visitors a “last seen in” status box.
#22: should be possible, but you will need something else. Your wordpress will need to store your IP address everytime you publish a post. Then this plugin can attempt to guess country of that IP address.
Will it work for the Netherlands too? Let’s see.
test test from norway
this is not Taiwan’s flag.
Thanks for the great plugin…love it!
-Adam
#26: Not my fault, I didn’t create those flags.
Hello from Guam?
Funny thing is that despite the number of users, US citizens seem highly unrepresented in the comments here. Wonder why? (Blah, blah, blah, if you want to flame me for that comment, visit my site, and don’t futz up Priyadi’s).
Cuba-cuba dari Malaysia.
[…] 都是國外網站….. 可以讓你知道你網路上的留言是從那個國家來的喔! http://priyadi.net/archives/2005/02/25/wordpress-ip-to-country-plugin/ 讓你對付Spam的好工具!! http://www.gudlyf.com/index.php?p=376
[…]
This is gonna look good on my forums… as soon as I figure out how to use it.
Priviet! From somewhere in Arizona.
After a long frustation that I was not able to load from the GeoIP CSV file (because of the lack of FILE priveliges in MySQL), I have loaded it on my local machine and created a SQL dump of the table. It is available at http://www.praveen.ws/journal/pub/iptocountry/iptocountry.tar.bz2
I thought of sharing this because it may be of some use to the people who face problems like me.
PS: BTW, I am not sure if the license of GeoIP allows to distribute the database in this way.
[…] ch 30th, 2005
IP to Country plugin is now up
Finally I have managed to get the IP to Country plugin up and running. The difficulty was because of the lac […]
For WP 1.5, we need to change the comments.php file in the current theme directory and add the following code to get it working.
#34: thank you for sharing!
#36: no you don’t need get_comment_author_IP() on the second invocation. if you use get_comment_author_IP(), then query will be done twice. if you don’t, it will use cached data from the last invocation.
saya dari singaparna. boleh test ya… mau coba pake plugins ini.
[…] Pluggins itu antara lain Browser Detection Plugin dan IP to Country Plugin. Gue bangga karena yang bikin ini adalah orang Indon […]
This is great! Thanks for all the hard work you’ve put in!
Good plugin
Hello
Test from Switzerland.
N1ce Day

IPstyle
Wow, nice
And a test from Hungary.
test from france
Mas Pri,
Saya kok tidak pernah bisa menampilkan gambar flagnya yah? Kalau saya pasang codenya yg utk flag selalu error. Tapi kalau tanpa flag sudah bisa sih. Kenapa yah? contoh yg di atas itu perlu ditambah prefix php apa lagi ya? saya sama sekali tidak tahu php nih. Mohon pencerahannya.
Terima kasih sebelumnya.
#46: bisa minta URL yang flag-nya selalu error itu?
Mas, ini URLnya. ini aku pakai contoh code yg ke dua di atas itu. Di sini aku hanya merubah path-to-flag foldernya thok. thanx yah…
http://blog.a2xm.com/?p=1#comments
PS: kalau hanya pakai yg ini:
nama country-nya bisa muncul dgn baik.
oops.. maksudnya yg ini:
–>
(muncul gak tuh)
oops.. maksudnya yg ini:
–>
(muncul gak tuh)
akhirnya…
Mas Pri,
Setelah saya kutak-katik lagi pelan2, ternyata sudah bisa jalan semua. Tapi pake code dr commentor #36. Maaf, sudah ngerecokin. Itu comments saya yg gagal tulis code dihapus saja bisa kan. Banyak sekali tuh. bikin kacau.
Terima kasih.
[…] 12; admin @ 7:44 pm
I’ve installed the IP to Country Plugin by Parida from here. There is a detailed explanation of the process in the link. Follow t […]
Just testing this great plugin…
pluign yang bagus… erm.. kalo mas priyadi tambah fungsinya pasti lebih bagus seperti digabungkan seperti:-
IP2Nation + Browser Detection + Gravatar = Webstat analysis
Pasti enak deh.. oleh demkian, plugin ini bisa bekerja 3 dalam 1….

#56.. aduh kalo bisa jangan digabung2 gitu deh, gak semua org butuh semuanya soalnya. Kalo dipisah kecil2 akan membuatnya jadi lebih flexible dan ramping. You can always mix and match from smaller components, but it’d be harder to do it the other way around, i.e. removing parts you don’t need.
Tested in Russia, Moscow!
[…] uck dan akhirnya tertarik sama aplus tadi siang … mulai install plugins smiley sama ip2country trus masang wp-photo yang udah diutak atik sama setan akhirnya […]
hi, kirimin script yang dah jadi donk.
Testing to see the functionality of this plugin
hello from China!
where am I?