In: , , ,
On: 2004 / 08 / 27
Shorter URL for this post: http://ozh.in/1t

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.

Shorter URL

Want to share or tweet this post? Please use this short URL: http://ozh.in/1t

Metastuff

This entry "IP to Nation WordPress Plugin" was posted on 27/08/2004 at 8:22 pm and is tagged with , , ,
Watch this discussion : Comments RSS 2.0.

377 Blablas

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

  2. […] 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 says:

    Love your script.

  4. […] ; 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. […] plicar o no los filtros del CMS antes de mostrarse. IP to Country: Versión del plugin […]

  6. […] 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. Excellent plugin my friend.. I shall be using this when I move to my wordpress blog at http://www.arrogantly.com

  8. Stephen says:

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

  9. […] 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. […] 표시만들기

    June 7th, 2005
    pionelle–>

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

  11. […] ´ì½˜ 만들기

    June 7th, 2005
    pionelle–>

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

  12. Victor alamo says:

    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 says:

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

  14. Victor alamo says:

    Hi,

    Here is the code

    Thanks in advance.

  15. Ozh says:

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

  16. Victor alamo says:

    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 says:

    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. […] ncy Tooltips Flickr Gallery flickrRSS Jerome's Keywords Kramer Subscribe to Comments IP2Nation Cat2Tag There are a few plugins beyond th […]

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

  20. TinyGuy says:

    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 says:

    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. abe says:

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

  23. andufo says:

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

    thanx

  24. Mitch says:

    Cool idea! :D

  25. Onur says:

    great job dude

  26. Mandar says:

    nice Work.

  27. Hoshi says:

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

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

  29. Rizwan says:

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

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

  31. sharon says:

    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

  32. fillupmybox says:

    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.

  33. Ozh says:

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

  34. fillupmybox says:

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

  35. Ozh says:

    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.

  36. fillupmybox says:

    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.

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

  38. adrian says:

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

    Secton is marked

  39. adrian says:

    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

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

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

  41. edhish says:

    Hi..

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

  42. Ozh says:

    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 ?

  43. edhish says:

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

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

  45. IcoBgr says:

    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

  46. Laptops says:

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

  47. Alan says:

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>
Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar and sign for a free account
Spam: Various spam plugins may be activated. I'll put pins in a Voodoo doll if you spam me.

Read more ?