IP to Nation WordPress Plugin

There it is, my very first real plugin for WordPress ("very first" meaning of course you can expect two or three more within the next 200 years). This plugin guesses your visitor's Country from his IP (check these examples). Based upon the very good ip2nation free data, I've simply packaged it as a plugin.

Set up the MySQL table

First of all, go to ip2nation and download the .sql file (I'm not mirroring it because it is updated from times to times, so get the latest one) This .sql file will create two tables, ip2nation and ip2nationCountries, containing top secret data you don't want explanations about :)

Newbie tips : first, install PHPMyAdmin on your website. This is a great interface for anything you can do with MySQL databases. Once it's done, look for the "SQL" link in the menu : you will be prompted for a .sql file, for example the one you just downloaded on your computer. It will set up and populate the required tables.

Download the plugin

Download the plugin :
ozhs-ip-to-nation.zip
Extract and upload to your blog, preserving directory structure if any.
Note: download counter here and stats on wordpress.org may differ and reflect the number of downloads before this plugin was hosted on the plugin directory

Use the plugin

I've created two "template tags" you can use in your pages :

  • wp_ozh_getCountryName() : prints the country name (France, Belgium, Vanuatu, stuff like this).
  • wp_ozh_getCountryCode() : prints the country "code" : 2 letters, as in top level domains (fr, be, it, stuff like this)

These two functions have to optional arguments :

  • $display : defaults to 0 (zero), if you want to print the result or just return it (see examples below)
  • $ip : defaults to $_SERVER['REMOTE_ADDR'], which is the visitor's IP address.

(Note : If you make a call to just one or to both template tags, it will cost you only 1 SQL query.)

(Note 2 : I named the plugin file and the function with a beginning "wp_ozh_" to prevent any duplicate function name, if someone ever creates another similar plugin)

Examples of use

For example, you could use the plugin with the following code :

  1. <?php
  2. echo "You are probably from ". wp_ozh_getCountryName() ."<br />";
  3. echo "If so, your country flag is  <img alt=\"your flag\"    
  4.   src=\"/images/flags/flag_"
  5.   . wp_ozh_getCountryCode() . ".gif\"><br />";
  6. ?>

The template wp_ozh_getCountryCode() goes well with this awesome tiny flags pack you've seen everywhere on the web (which were originally created by a guy I know, Zarkof, and are free to use, which is a nice gift considering the amount of pixel skills and time he must have put in these 175 flags :)

Passing the parameter "0" (zero) to a function could be useful for example to test a language redirection without printing anything. The following example determines wether the user is supposedly speaking French or not :

  1. <?php
  2. $country = wp_ozh_getCountryName(0) ;
  3. switch ($country) {
  4.    case "France" :
  5.    case "Guadeloupe" :
  6.    case "Luxembourg" :
  7.    case "Monaco" :
  8.    case "Martinique" :
  9.    case "New Caledonia" :
  10.    case "French Polynesia" :
  11.    case "St. Pierre and Miquelon" :
  12.    case "Reunion" :
  13.    case "French Southern Territories" :
  14.    case "Wallis and Futuna Islands" :
  15.         echo "you speak French";
  16.         break;
  17.    default :
  18.         echo "you may not speak French";
  19. }
  20. ?>

You can also pass an IP address to the functions, which could be used to print your commenters' country : in blogroot/wp-comments.php (WP 1.2) or blogroot/wp-content/themes/yourtheme/comments.php (WP 1.5+), where you see <?php comment_author_link() ?>, put the following :

  1. <?php comment_author_link() ?> from
  2.  <?php wp_ozh_getCountryName(1,$comment->;comment_author_IP) ?>

This will output something like : JohnDoe from India

To put a tiny flag by the name (like in comments here), you could use the following code :

  1. <?php
  2. echo '<img alt="your flag" src="/images/flags/flag_'
  3.      . wp_ozh_getCountryCode(0,$comment->comment_author_IP)
  4.      . '.gif">' ;
  5. ?>

Disclaimer

I am not positive that the ip2nation data produce a 100% correct result. You may happen to get generic or incorrect results, such as "Europe" or "US Educationnal" instead of a country. You might tell the author of the data, I'm just a packager :)

Sandbox, testing and debugging help

Please, don't post a comment here just to check what flag will come up for you. To do so, please have a look at the examples and demo page, as stated in the very first line of this article. Test comments will be removed and their author won't start with a good karma if they ask for my support afterwards.

Please, be accurate and give details when you're asking for support. "The plugin doesn't work, can you help ?" is completely useless yet very frequent. State the PHP code you use and the error or unexpected output it generates.

Please, don't post multiple lines of code here when you're asking for support or debugging help. Upload your code somewhere I can check it, or use this site to paste your code and link it here.

And please, please, please … read the instructions and examples here before asking something that is already answered and explained here.

377 comments

  1. My Other Side of the Stories » Working out…

    […] 7;s Browser Sniff plugin to detect visitor's browser and OS. Installing Ozh's Ip united.com">2 Nation plugin to det […]

  2. My Other Side of the Stories » Wordpress Plugin Meme

    […] post. (9/10) WP Plugin Manager, useful, especially when it came to my lazy times (8/10) IP2Nation, look useful It shows wrong nation flag sometime (7/10) […]

  3. Mary

    Love your script.

  4. Ingoal’s Insight… » Plugin-Time

    […] ; Ingoal @ 18:55

    Today I took the time and installed the following plugins: IP to nation which will show a country flag – guessing the origin of th […]

  5. empdesign » Blog Archive » Plugins Wp

    […] plicar o no los filtros del CMS antes de mostrarse. IP to Country: Versión del plugin […]

  6. day by day » Blog Archive » dandanin WP

    […] ravatar di comment .. diajarin ibu ini 2. AVATAR POP UP di post .. ini lihat pluginnya di Ozh       ini contohnya : Me 3. Smilies di COmment .. li […]

  7. Unadulterated Arrogance

    Excellent plugin my friend.. I shall be using this when I move to my wordpress blog at http://www.arrogantly.com

  8. Stephen

    This plugin is great as it adds color to the overall site. Superb!

  9. andrew’s sandbox » Blog Archive » My WordPress Setup

    […] ust using it to provide that random G2 Image you see on the right hand side of this page. IP 2 Nation – My latest addition to my blog. Comments now have the nati […]

  10. Blooming Town » Plugin: ip로 국가아이콘 표시만들기

    […] 표시만들기

    June 7th, 2005
    pionelle–>

    – 참고: IP to Nation WordPress Plugin by Ozh 설명히 자세하게 나와있ì […]

  11. Blooming Town » Plugin: ip로 국기아이콘 만들기

    […] ´ì½˜ 만들기

    June 7th, 2005
    pionelle–>

    – 참고: IP to Nation WordPress Plugin by Ozh 이 Plugin을 달면 상대방의 […]

  12. Victor alamo

    Hi,

    I have installed the plugins in website , plus "activate". Apart from that, I have place the flags directory under my Blog. Still, I can not get show anything in :

    1.- Your probably from "empty"
    2.- If so, you country flag is "empty"

    I have the check the path and it is correct, but I have noticed that function can not identify which flag to display "flag_.gif"

    Couuld you please, give me some guidance ?
    I will really appreciate , i have spent 3 hours already with this plugin.

    Thanks again.

  13. Ozh

    Well, first tell me what code you tried to use …

  14. Victor alamo

    Hi,

    Here is the code

    Thanks in advance.

  15. Ozh

    These 2 lines look correct… So the problem is somewhere else in your page.

  16. Victor alamo

    Hi again,

    One question: did you place the code inside the comments.php? which it locates in wp-content/themes/mythemes/comments.php .

    For my case, I just copy & paste in that particulars file and add the open & close for
    Oh! – I make the plugins activate.
    That was all!.

    Thanks again.

  17. Victor alamo

    Hi,

    Hi again,
    All I see, it is that the function in code is being call, but it is not returning any value back to link , therefore…it just result in "/flags/flag_.gif" inside link to obtain the right flag.

    my main file is locate under plugins as you have mentioned in your website.

    Thanks again.

  18. matthewstinson.net » Smooth sailing

    […] ncy Tooltips Flickr Gallery flickrRSS Jerome's Keywords Kramer Subscribe to Comments IP2Nation Cat2Tag There are a few plugins beyond th […]

  19. Blooming Town » Plugin: ip로 국기아이콘 만들기

    […] – 참고: IP to Nation WordPress Plugin by Ozh 이 Plugin을 달면 상대방의 ip에 따라 저장되어 있는 170여개의 이미지에 따라 국가별 국기가 아이디 옆에 표시가 된다. 설명히 자세하게 나와있지만, 일단 영어이므로 쉽게 한글로 설명해보자. […]

  20. TinyGuy

    This would be great if it worked!
    Unfortunately mySQL throw errors at me when trying to run the sql file… "Exceeded 5000 commands" or something like that :(

  21. Ozh

    TinyGuy » doh… too bad for you. This seems to be a misconfiguration of your host, try to get in touch with your web admin.

  22. eticketride.org » ip2nation

    […] […]

  23. abe

    ehmm.. Ozh..
    I've just moved to WPress!
    Well, i aim to use your plug-in(s), thanks dude! ^_^

  24. andufo

    i am using the plugin as well. i did make some modifications for it to work better with my weblog though…

    thanx

  25. Mitch

    Cool idea! :D

  26. Onur

    great job dude

  27. Mandar

    nice Work.

  28. Hoshi

    Very Cool script! It works very well!! :D

  29. 苟世录 » Blog Archive » IP to Nation

    […] WordPress显示留言者地区的插件IP to Nation。 […]

  30. Rizwan

    Hey man, great plugin, really appreciate all the work you must've put in. Thanks again.

  31. 岁 月 æ—  声 » Blog Archive » WordPress Plugin: IP to Nation

    […]   IP to Nation,可以把来访者的IP转为所在实际区域。 […]

  32. sharon

    Someone could post script that integrate google maps api from ip2nation site.
    I miss on their site finally implementation of how it works.
    They said look to the source.Ok but where is code that read sql IP/country and then display flags on map.
    I'm confused.If it is so easy would be nice to post completly code for integrating with google maps api.

    Thanks

  33. fillupmybox

    I've received this error:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

    I Hope you can spare a moment and take a look at it.

    The country name showed up perfectly fine, just the image is giving me the trouble.

  34. Ozh

    when you echo "something", escape the quotes inside something

  35. fillupmybox

    Sorry, I don't quite get it. I'm not much a technical savvy guy. Hope you can explain it in details.

  36. Ozh

    Quotes cannot be nested :
    echo "my name is "tom" and I'm cool" : wrong
    echo "my name is \"tom\" and I'm cool" : ok

    Putting a \backslash before a quote is called escaping the quote so that this quote does not behaves as a trailing closing quote.
    You have to escape the quote inside what you're echoing :
    echo "<img src=\"bla\">" : ok
    echo "<img src="bla">" : not ok.

  37. fillupmybox

    It's working now; thanks for the help. :)

    I just want to point out that there is no backslash in the 4th example, so I thought it was not needed. My bad.

  38. Word Press Plugins for ProBloggers: Blog Tips at ProBlogger

    […] Better Feed Click Counter IP 2 Nation Random Words […]

  39. adrian

    Hi
    Great looking plugin, but i am not getting any flag images
    Code used here

    Secton is marked

  40. adrian

    Oh and i added the (0) in the country just to test, but have also left it as () too, and still no joy, Added the to tables to SQL and populated them with the ip nation data file, the image that it tries to point to does NOT include the country code, ie its pointing to /wordpress/flags/flag_.gif

  41. Love ở Việtnam

    顯示訪客國別外掛 — IP to Nation

    這個外掛是利用ip2nation.com的資料庫寫出的, 用以顯示訪問者國別名或國家代碼, 也可以進一步利用各國旗幟來直接代表.
    這對於網站常有不明人士的拜訪留言者 , 可以利用此外掛, 顯示出…

  42. edhish

    Hi..

    just installed the plugin, but the flag won't show up :(
    everything is right.. dunno what is the problem.. help please

  43. Ozh

    edhish » obviously, everything is not right, since it doesn't work. And how am I supposed to help if I have no idea of how and where you did what ?

  44. edhish

    ups sorry.. there is the right URL. I already upload everything according the instructions.. do you need the code that i put ??

  45. edhish
  46. Leo Blog » WordPress插件搜集(转帖)

    […] IP to Nation(来访者的IP转为所在实际区域) […]

  47. IcoBgr

    Hi,
    I found this plugin and I'm tring to install it on my blog.
    But when I start the plugin I get blank page when I try to open the blog. I use WordPress 2.0.

    best regards

  48. Laptops

    Thanks for it …was looking for a similar plugin ! :)

  49. Alan

    Great idea, this will be added to Hacked Gadgets soon!

  50. TEST » IP to Nation

Comments are closed.