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. nder: Computers &#8212; site admin @ 12:16 am

    Just added a plugin, created by Planet Ozh, that guesses the origin of a comment by its IP address. So […]

  2. Marc says:

    I have installed this plugin and everything is OK, with one exception. The flag gifs that display are only for the Philippines, the country where I'm located. The comments from other countries show the same PI flag.

    The site is the, Cranial Cavity weblog.

    Any ideas to cure this problem.

  3. Ozh says:

    where & what code did you use ?

  4. Thorin says:

    I have installed the plugin, created the tables but I'm not sure where to put the code.

  5. Ozh says:

    well … it depends what you want to do with the plugin … The code needs to be in a .php file within php tags (<?php and ?>). Just copy & paste the examples I give, or be more specific with your question if you can't find the answer here about what you want to do.

  6. Thorin says:

    I'd like to display flags after comments. I guess I can copy and paste your first example (?) but where? wp-comments.php?

  7. Ozh says:

    You should probably read the last paragraph of this article.

  8. Thorin says:

    I've read the whole article and all of the messages. It would help beginners if you could spell it out a little more clearly.

  9. Ozh says:

    to print your commenters' country : in wp-comments.php, where you see <?php comment_author_link() ?>, put the following :

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

    Doesn't this answer your question ?

  10. Thorin says:

    I guess the plugin isn't compatable with WordPress 1.5. There is no file in 1.5 called wp-comments.php.

  11. Ozh says:

    The plugin is compatible, you just have to guess what changed with 1.5 ..
    A quick look in /wp-content/themes/(your theme or default)/comments.php is enough to guess you have to modify things here.

  12. jays says:

    very cool script!!! ill use it!

  13. Bryan P says:

    very cool script you have hear. i eventually got an overseas person to test my script out but it only shows one country. ie my country as their flag. I followed everything pretty closely.. Is there something i missed.
    I'd really like to get this working. Thanks Mate!!

  14. Ozh says:

    Please be more accurate, I can't help with so few info. What code did you use, and where did you put it ?

  15. Bryan P says:

    I didnt put the comment_author_link edit you gave. I thought that was optional
    In the end i decided to put that alongside the code where you said to place it and the rest of the code
    which was the wp_ozh_getCountryName which i modified a little bit in the comments section.

    Anyways without the comment_author link edit, it would show all users as australian. Now that its there, it works. Do i really need this to get it work?
    From how i read it i thought it would show the ip. I rather that not be the case.
    It works tho and i thank you for your time and im sorry for my misintepretation of what you wrote there.
    I just assumed it wasn't necessary.

    If i double post my mistake it wouldnt let me put in the code that i used.

  16. Bryan P says:

    hi its me again. after posting that comment_author_link
    it now says like bryan is from australia says:
    instead bryan says:
    i rather have it say bryan says:

    though it shows the flags correct for the overseas person, my first comment i made, ended up saying i was from finland but the other test post came out correct.
    wierd. any suggestions?

  17. Ozh says:

    Well, can't say much more than I already explained in this page, dude. You can pass 2 parameters to wp_ozh_getCountryName, the first one setting if you want to echo the result or not, the second one being an IP, defaulting to current visitor's IP if nothing stated …

    If at least you provided an URL where to look at …

  18. Bryan P says:

    it's cool i just modified it a little bit to make it work the way i wanted it. thanks.

    here is what i did. just moved a few things around til i got what i wanted aesthetically

    I ended up moving the echo away from this code so that it wouldn't repeat the country again.

    wp_ozh_getCountryName(1,$comment->comment_author_IP)

  19. littleoslo says:

    it seems very good n now i try to test to see a norwegian flag come up or not, :-)

  20. Ozh says:

    It works. It clearly shows that you're in the Austrian part of Norway.

  21. star says:

    excelent, thank you

  22. Alejandro says:

    Hi there!!
    This is amazing!! really cool! Althou phpmysql is better it deppends on the server because not all of them can manage 18000 queries hehe. Anyway, i wanted to show not the name but the flags in the comments so I managed to get this:

    comment_author_IP) . ".gif">"; ?>

    But then i get for example for Peru: pe[Peruvian flag] How do I get rid of the prefix? for Spain i get the "es" and so on…
    Thanks in advance :)

  23. Ozh says:

    Don't understand what you're asking for. I think that examples provided here explain how to display ieither a flag or a comment name, just follow the guidelines detailed here.

  24. Alejandro says:

    I have this in my code:

    <? echo "<img alt="your flag" src="/images/flag_" .
    wp_ozh_getCountryCode(1,$comment->comment_author_IP) . ".gif">";
    ?>

    But then, the comentor get this http://weblog.homelinux.org/archivos/2005/02/17/65/#comments
    See the country code before the flag.

    Saludos
    Alejandro

  25. Ozh says:

    This is exactly the code I have here. The code you pasted outputs only an image tag, so you obviously have something *before* it. Paste your complete code somewhere I can read it (not here)

  26. Alejandro says:

    hehe, as you see, i have wp_ozh_getCountryCode(1,$comment->comment_author_IP) but playing a little bit, i ended changing that 1 to 0 and the country code :) sorry to bother
    merci :P

    Alejandro
    PS: good idea isn't it? i see you've also implemented it :P

  27. Alejandro says:

    "(…) and the country code was gone (…)"

  28. Ozh says:

    I had it implemented since the day I wrote this plugin. It just disappeared when I upgraded WordPress and overwrote the comments.php file :)

  29. Alejandro says:

    Oh! I didn't knew that :) but it is a cool thing. I might be changing it to "writing from" because I'm from Peru, but living in Finland….so sometimes the flag is not where we come from :P well it's just a thought :)
    Saludos
    Alejandro

  30. Jewel says:

    I am sorry but I cant get the flag and the name to appear. I want my comments form to say Comment by Jewel from (flag) United Kingdom, but I cant get the flag to appear at all. I have put the flags in "http://wwww.jewelswebgraphics.com/news/wp-images/flags" and have used an absolute url to call it but to no avail. When I copy and paste your example code, I keep getting parse errors.
    Can you help at all please? (php newbie)
    thank you

  31. […] In an effort to add more flair to this site, I have incorporated Ozh's IP to Nation Plugin which kindly tells me which country som […]

  32. Ozh says:

    Jewel » obviously with no info on your "parse errors", I can't do much. Paste somewhere (*not here*) the code you're using and mail me back when it's done

  33. Prz says:

    I'm soor for the last comment, the error is: img alt="
    Fatal error: Call to undefined function: wp_ozh_getcountryname() in /usr/home/sakrajda/domains/sakrajda.info/public_html/log/wp-content/themes/default/comments.php on line 30

  34. Ozh says:

    It seems you didnt activate the plugin.

  35. jesus_ says:

    I'm gonna test it with 1.5, and try to find some more cool stuff to do with it :)

    Very good idea to put that in a plugin btw. Great thinking (AND great explanation for non-coders!).

  36. jesus_ says:

    It works great with 1.5, although here are some remarks:

    1.5 uses themes, so the file you want to edit is "/wp-content/themes/default/comments.php" or in whatever directory your theme is.
    An example of the code you could use to display a flag before the commenters name:

    comment_author_IP). '.gif'; ?>" width="18″ height="12″ alt="comment_author_IP); ?>" /> says:

    Don't forget to change the path's to your flag-images.

    Great plugin, thanks a lot!

  37. […] kmark" title="Permanent Link: "> I was searching for cool plugins and found this one, it looks up the country an IP originates from (by using ip2na […]

  38. […] 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 […]

  39. Max says:

    cool stuff

  40. Claire says:

    I think you should specify in the instructions to add the SQL file to your WordPress database — and that you don't need to create a new one like I did :-P

    Anyway, great plugin!

  41. […] « Bad Taylor. | IP to Nation. Well, I finally got the IP to Nation plugin working after some instruction problems and IP to C […]

  42. […] lag next to their name. Check out this article from the guy who packaged it for WordPress I followed his instructions completely and they worked for me, a coup […]

  43. […] erInfo v1.8 2. ip2nation (Per Gustafsson Consulting) for country codes and names tables 3. Ozh for his IP to Nation plugin Download j […]

  44. Prz. says:

    Is it possible to fix bad recognizing IP? Both IP 202.81.205.22 and 221.134.1.43 are localized in India, but the second one has got Vietnamese flag. ;)

  45. […] 自行換上臺灣國旗,真受不了中國到處都要打壓的惡行! 相關網址:IP to Nation WordPress Plugin 此篇文章發è […]

  46. xiaolei says:

    cool, i will promote it among my friends

  47. roy rojas says:

    this plug in it's cool but don't work with many countryes, for example this…nicaragua or some ips of costa rica

  48. […] Sepertinya gue harus nyari alternatif laen,.. Namanya IP TO NATION. Katanya seh sama aja. Yang ngebedain itu cuma databasenya […]

  49. Ozh says:

    Again, please DO NOT post junk comments just to test this fu**ing plugin. Have a fu**ing look at the example page mentionned at the beginning of the page. God I'm tired of deleting comments everyday.

  50. garnet says:

    I've uploaded the sql file using phpmysql, and the sqlquery confirms the 18,000 instrustions, but no table was created. Can I create one manually? How many fields? etc. I'm unable to upload the zip file, had to unzip then upload. Sorry for this question, but can't use the plugin without the table.
    Thannks for any help
    Garnet

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 ?