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
.
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?
[...] WordPress, con la posibilidad de aplicar o no los filtros del CMS antes de mostrarse. [...]
mas Pri.. tutorial tuk newbie doong
saya nga ngerti cara no. 3dan 4 nya mas… tulung
#66: itu berhubungan dengan database mysql, coba hubungi sysadminnya. saya gak bisa kasih info detail karena tiap sistem beda-beda konfigurasinya.
Good job
But it’s a little too long installation process
[...] ugin Posted in Work, Asides by Joachim on the May 25th, 2005 I just installed the IP to country plugin. I’d say the difficulty to install th [...]
Pri, punya gue kok anehnya, semuanya from Indonesia.. Ada yang salah ?
Interesting plugin. :3 May try it one day when I’m feeling not that lazy. ^^
test…let me test this…
Ahhh, mwahaha, I posted on May 31th, not June 1st…at least the time is correct.
#74: June 1st on GMT+7, I think your timezone is GMT-5 (you post on 8:04AM)
#74: not everyone lives in the US/Canada Central (GMT-5) timezone.
hi, I love this plugin alot but cant get it to work. I notice that the table I created has no index… is that right?
Thanx
Hi, I love the plugin, but cant get it to work. I have a feeling it might be mysql related. the fields are populated with this:
Hi, i was able to get this working but had to change the following code to get it to display right:
comment_author_IP); ?>" src="/flag/.gif" />thanks for the awesome plugin
sorry, the code didnt post correctly.
Shalom from Israel!
[...] t be sent. Semalam suntuk Gara gara om priyadi nih dengan pluginnya jadi begadang semalam suntuk. Udah gitu pluginnya ga jadi pula , [...]
[...] yah buat blog, banyak hal yang tadinya gak tau sekarang jadi tau . Setelah install plugin IP to Country nya om Priyadi dengan susah payah sampe berdarah darah . Sek [...]
Hey,
Well this worked for me:
PHPMYADMIN > SQL Tab > Insert data from a text file into the table > DATA LOCAL LOAD method
Take Care,
Will
ok Like this
:
LOAD DATA LOCAL INFILE ‘/path/to/ip-to-country.csv’ into table iptocountry fields terminated by ‘,’ enclosed by ‘”‘ lines terminated by ‘\r\n’;
Very cool, I’d love to have this plugin..thanks!
sits
[...] Interessante WordPress tillegg - Splitter kommentarer på delsider - eksempel1, eksempel2 - Vi satser vel på mange kommentarer
- Kramer, Henter lenker knyttet til en artikkel fra Technorati uten at det er sendt pingback til Ensporet. - Stavekontroll - bør kjøres gjennom denne før en publiserer. - IP til land - greit å kunne bruke til å vise flagg fra hvor leseren befinner seg. [...]
[...] ã ã„ã¶å‰ã«ã€æ›´æ–°æ»žã£ã¦ã„る英語サイトã«å…¥ã‚Œã¦ã„ãŸIP to Country。ç§ã®è‹±èªžã‚µã‚¤ãƒˆã®URL変更ã«ä¼´ã£ã¦ã€ä½¿ã£ã¦ã„ãŸCVSデータを更新ã—ãŸã®ã§ã‚¤ãƒ³ã‚¹ãƒˆãƒ¼ãƒ«æ–¹æ³•も書ã„ã¦ãŠã“ã†ã¨æ€ã„ã¾ã™ã€‚何ã§URLを変更ã—ãŸã‹ã¨è¨€ã†ã¨ã€ã‚µãƒ¼ãƒç§»å‹•ã®è¨˜äº‹ã§æ›¸ã„ãŸæ–¹æ³•ã®ã‚µãƒ–ドメインを利用ã—よã†ã¨æ€ã£ãŸã‹ã‚‰ã§ã™ã€‚今ã¾ã§ã¯ãƒ«ãƒ¼ãƒˆã«”en”ã¨ã„ã†ãƒ‡ã‚£ãƒ¬ã‚¯ãƒˆãƒªã‚’作ã£ã¦ã„ã¾ã—ãŸãŒã€ã‚µãƒ–ドメインã«ã™ã‚‹ã®ã§ãれも”blog”ã«å¤‰æ›´ã—ã¾ã—ãŸã€‚ã¨ã„ã†ã®ã¯ã€ã‚†ãゆãã¯è‹±èªžä»¥å¤–ã®è¨€èªžã§ã‚‚勉強ã®ãŸã‚ã«æ›¸ã„ã¦ã„ããŸã„ã‘れã©ã€ç‹¬ç«‹ã•ã›ã‚‹ã»ã©ãƒã‚¿ã‚‚言語力もãªã„ã®ã§ã€è‹±èªžã¨æ··åŒã—よã†ã¨æ€ã£ã¦ã€‚ ã•ã¦ã€IP to Countryã§ã™ãŒã€ã“れã¯ã€ã‚³ãƒ¡ãƒ³ãƒˆã‚’ã—ã¦ã‚‚らã£ãŸã‚‰ãã®äººã®IPã‹ã‚‰å‡ºèº«å›½ã‚’割り出ã—ã¦ãã®å›½æ——アイコンを表示ã•ã›ã‚‹ãƒ—ラグインã§ã™ã€‚日本語ã®ã‚µã‚¤ãƒˆã«ã¯æ—¥æœ¬äººã—ã‹æ¥ãªã„ã ã‚ã†ã‹ã‚‰ã‚³ãƒ¡ãƒ³ãƒˆã«æ—¥æœ¬ã®å›½æ——を並ã¹ã¦ã‚‚ãªãã¨ã„ã†ã“ã¨ã§ã“ã®ã‚µã‚¤ãƒˆã«ã¯å…¥ã‚Œã¦ã¾ã›ã‚“ãŒã€é¢ç™½ãã†ã ã£ãŸã®ã§è‹±èªžã‚µã‚¤ãƒˆã§å…¥ã‚Œã¦ã¿ã¾ã—ãŸã€‚ ã¾ãšã€ã“ã®ãƒ—ラグインã¯IP to Countryã®ãƒ‡ãƒ¼ã‚¿ãƒ™ãƒ¼ã‚¹ã‚’利用ã™ã‚‹ã®ã§ã€IP to Countryã®ãƒšãƒ¼ã‚¸ã®”latest IP-to-Country Database”ã®ãƒªãƒ³ã‚¯ã‹ã‚‰CVSファイルをダウンãƒãƒ¼ãƒ‰ã—ã€ãƒ•ァイルを解å‡ã—ã¦ãŠãã¾ã™ã€‚WordPressã§ä½¿ç”¨ã—ã¦ã„るデータベースをphpmyadminã§é–‹ãã€SQLタブメニューを開ã„ãŸã‚‰ã‚¯ã‚¨ãƒªãƒ¼å®Ÿè¡Œã®ãƒ†ã‚ストエリア㫠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)); ã¨å…¥åŠ›ã—ã€å®Ÿè¡Œã—ã¾ã™ã€‚ã“れã§ã€IP to Country用ã®ãƒ†ãƒ¼ãƒ–ルãŒä½œæˆã•れã¾ã—ãŸã€‚ 次ã«ã“ã®ãƒ†ãƒ¼ãƒ–ルã«å…ˆç¨‹ãƒ€ã‚¦ãƒ³ãƒãƒ¼ãƒ‰ã—ãŸCVSã®ãƒ‡ãƒ¼ã‚¿ã‚’挿入ã—ã¾ã™ã€‚phpmyadminã®å·¦ã®ãƒ•ãƒ¬ãƒ¼ãƒ ãƒ¡ãƒ‹ãƒ¥ãƒ¼ã«æ–°ã—ãã§ã㟔iptocounry”ã‚’é¸æŠžã—ã¾ã™ã€‚é–‹ã„ãŸãƒšãƒ¼ã‚¸ã®ä¸€ç•ªä¸‹ã®ã»ã†ã«ã‚ã‚‹”テーブルã«ãƒ†ã‚ストファイルã®è¿½åŠ ”をクリックã—ã¦é–‹ãã¾ã™ã€‚ é–‹ã„ãŸãƒšãƒ¼ã‚¸ã®”テã‚ストファイルã®å ´æ‰€”ã«å…ˆç¨‹ãƒ€ã‚¦ãƒ³ãƒãƒ¼ãƒ‰ã—ã¦è§£å‡ã—ãŸCVSファイルを呼ã³å‡ºã—ã¾ã™ã€‚ã‚ã¨ã¯”フィールド区切り記巔を「,ã€ã«ã€ãƒ•ィールド囲ã¿è¨˜å·ã‚’「”ã€ã«ã€è¡Œã®çµ‚端記å·ã‚’「nã€ã«ã—実行を押ã—ã¾ã™ã€‚å°‘ã—æ™‚é–“ã¯ã‹ã‹ã‚‹ã‹ã‚‚ã—れã¾ã›ã‚“ãŒã“れã§ãƒ‡ãƒ¼ã‚¿ã®æŒ¿å…¥ã¯å®Œäº†ã§ã™ã€‚難ã—ã„å ´åˆã¯Praveen’s Journalã¨ã„ã†ã‚µã‚¤ãƒˆã§sqlファイルãŒãƒ€ã‚¦ãƒ³ãƒãƒ¼ãƒ‰ã§ãã‚‹ã¿ãŸã„ã§ã™ã€‚ ã‚ã¨ã¯ãƒ—ラグインをダウンãƒãƒ¼ãƒ‰ã—ã¦ãƒ†ã‚ストエディタã«ã‚³ãƒ”ペã—〔wp_ip2country.php”ã¨ã„ã†åå‰ã§ä¿å˜ã—ã¦ã„ã¤ã‚‚ã®Pluguinsフォルダã«ã„ã‚Œã‚¢ãƒ‰ãƒŸãƒ³ã‚¨ãƒªã‚¢ã§æœ‰åŠ¹åŒ–ã—ã¾ã™ã€‚ ã‚ã¨ã¯comments.phpã«ã“れを呼ã³å‡ºã™ã‚³ãƒ¼ãƒ‰ã‚’書ã‘ã°å®Œäº†ã§ã™ã€‚ wp_ip2c_getCountryName($display, $ip) 国ã®åå‰ã‚’表示ã—ã¾ã™ã€‚(例:Indonesia) wp_ip2c_getCountryCode2($display, $ip) 2æ–‡å—ã®çŸç¸®å½¢ã§å›½ã®ã‚³ãƒ¼ãƒ‰ã‚’表示ã—ã¾ã™ï¼ˆä¾‹ï¼š id) wp_ip2c_getCountryCode3($display, $ip) 3æ–‡å—ã®çŸç¸®å½¢ã§å›½ã®ã‚³ãƒ¼ãƒ‰ã‚’表示ã—ã¾ã™ï¼ˆä¾‹ï¼š idn) [...]
[...] IP 2 Country [...]
[...] If you’re interested in implementing it in your own WordPress blog, the plugin can be found here. [...]
works great…!!
A bit more complicated than the plugins I usually use, but well worth it! Thanks!
OK well, I had a couple of problems with the coding, but I fixed them and the plugin works great now. Thanks again!
[...] I also unsuccessfully tried to install three different country detecting plugins. [...]
Testing before I (attempt) install…
Asheville, NC USA

Salam from Saudi Arabia,
Thank you for your help
checking the plugin
hello from Mauritius…
aww. doesn’t work for me