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. […] led under: General — Jez @ 13:01 pm — 0 day(s) old Just found a neat ip2nation plugin for WordPress by ozh, which I decided would be rather […]

  2. […] n Plugin Categories – Code Cool Scripts WordPress Hack LinkyLoo — Ozh WP Plugin : IP to Nation : this plugin guesses your visitors' cou […]

  3. […] n slvShowNewIndicator(1093965557); Filed under: WordPress Plugins|Google it! This plugin guesses your visitor's country from his IP and can displa […]

  4. […] a — dario @ ore piccole (1:38)

    Ho installato un plugin che dovrebbe capire il paese di provenienza dei commentatori. […]

  5. […] ment flags
    Filed under: blogging geekery — shaky @ 1:50 pm

    Added the IP to Nation WordPress plugin to add a little flag next to your name in […]

  6. R@lf says:

    I want to add a special flag for my own IP. But now I've got one little problem, I don't know how to add my IP in the SQL table. I know how to add but I don't know what I should add? The full IP?

  7. Ozh says:

    Hmm, nice idea, I'll have to think about doing this too, like adding your favicon instead of your country flag :)

    I dont think the easiest way is to mess with the ip2nation sql tables : as you may have noticed, they're a bit "osbfuscated"

    It's a lot easier to test first comment->comment_author to check whether it's your nick or not, to insert either your personnal .gif or call wp_ozh_getCountryName

  8. w8in says:

    Ozh, could you give me a sample code of how to implement the flag in the WordPress comments? I've got the country working in print in there, but can't seem to get the flag to work.
    Thanks !

  9. Ozh says:

    Look at the examples given here, it's all explained …

    The code from my own comments page is :

    <img alt="<?php wp_ozh_getCountryName(1,$comment->comment_author_IP) ?>" src="/images/flags/flag_<?php wp_ozh_getCountryCode(1,$comment->comment_author_IP) ?>.gif">

  10. w8in says:

    I didn't see the flag in the comment field explained, but maybe I'm just stupid, blind and ignorant ;-)
    Thanks for the sample and I apologize for posting twice :-)

  11. Ozh says:

    no problem :)

  12. Ged says:

    I have got a problem.
    Script displays only one country flag…
    All people are showed as come from Poland but ther are not.
    Could You help me please?

  13. Ged says:

    Well flags near to commentator name is always the same and it displays curent visitor country.

  14. Ged says:

    ok, sorry for panic,
    I have managed it

  15. Ozh says:

    Cool :)

  16. dr.gs says:

    It's terrific, I'm putting this script to put visitors flags on comments, like you here, to mark links according to their server's country… to provide specific content to users…

    thanks!

  17. dr.gs says:

    again me… just to say that some after installing on one of my blog some country wasn't showing up, so I put this into the country's tag:

    alt="comment_author_IP) ?>">

  18. How to set at the comment sessian translate text to img like this comment???

  19. Ozh says:

    ??
    Sorry, didn't understand …
    If your question is "how do put flags next to commenters' names in the comments page", read here, it's explained.

  20. novalis says:

    In my server i only manage one msql database, i can't open another one , what can i do?

  21. Ozh says:

    What is the problem. You're supposed to create a *table* in your *database*, just as another WordPress table. Not another db.

  22. novalis says:

    I have my wordpress in tripod lycos server they give you only one database (msql and php4)for you an they no give you to create tables , only modify or delete them, if you want to create tables you cando with an intall.php (instaler) but i don't know hoe to create them.. can you help me?

  23. novalis says:

    sorry, the PHPMyAdmin acceed to create new tables
    it says

    Create new table in you database
    Name:
    fields: (continue)

    but i don't know what to put there i can't , with lycos i can't open another database (ipnation.sql file) so i can't set up and populate the required tables

  24. Ozh says:

    Ooooook. So : you installed phpmyadmin. Now, select your database, you should have a link name "SQL" at the top of the main frame. Click, now there is a field with a "Browse…" button, select the .sql file, and click ok.
    And if you don't have this in your phpmyadmin, well, time to move on a real host where you can install a proper phpmyadmin version.

  25. Frk says:

    It seems really cool!!!

  26. novalis says:

    sorry, i can't.. and who can install phpmyadmin in a new server?

  27. novalis says:

    I have find a solution, the file was too large to load up to the server , i have upload files in ten times

    thanks!! it is very nice

  28. Jauhari says:

    Nice Plugins in this work very well on my site, I use WordPress 1.3 alpha x

    ^_^

  29. Ozh says:

    thx :)

  30. novalis says:

    you don't have the flag of Chile

  31. KarlanKas says:

    Muchas muchas muchas muchas gracias!!!
    Lo puedes ver funcionando en mi blog!
    Es fantástico!

    Thank you very very very very much!!!
    You can see it working on my blog!
    It's marvelous!

    Merci bocu, merci bocu, merci bocu…
    Je ne parle pas francais… Je suis désolé

  32. Ozh says:

    Well… Soy encantando que el plugin te gusta :-P

  33. tono says:

    I am totally blind about php and/or sql.
    so, please tell me how to execute the ip2nation.sql to create the tables to my database ?

    really appreciate your help.
    btw, it's display correctly my country and it;s flag. great job :)

  34. Ozh says:

    Tono : first, install PHPMyAdmin, this is a very handy online interface to admin your MySQL database
    Then, you'll find a "SQL" menu which will prompt you for a .sql file (for example ip2nation.sql you'll have downloaded on your PC) and which will do the job.
    Hope this helps

  35. dario says:

    Hi Ozh,
    thanks for your comment on my blog. Flags are now displayes correctly, but there's the country code before the flag. Do you know what did I do wrong?

  36. shaky says:

    Seems like trackback pings always show as USA. Anyway, great plugin, thanks!

  37. Sara says:

    Thank you for the great plug-in! Works great!

    You may want to include what you do with the .sql in your instructions. I knew what to do, but others may not. :)

  38. Ozh says:

    I think trackback flags are correct, i.e. it shows where your website is hosted (look at 1st tb, the flag is Canadian)
    Sara, smart suggestion, I'll include a short explanation about how to deal with the .sql :)

  39. shaky says:

    re TBs … OK, got it.

    One other question, the flag images, are you supposed to leave them g-zipped? They display fine for me in Firefox on a Mac as gif.gz, but not in Safari or IE on the Mac.

    Thanks.

  40. Jen says:

    hey! great plug-in, thanks! it's really interesting to see where everyone's from :)

  41. Ozh says:

    Shaky : no, unzip the pack somewhere in your website (/blog/flags/ for example) and use them as in the first example

  42. Jean-Pascal says:

    salut, je suis du québec et on est quand même près de 7 millions de francophones ici! peut-être faudrait-il ajouter le canada dans votre exemple "wether the user is supposedly speaking French or not". en tout cas, moi ça m'a surpris quand j'ai eu le message "you may not speak french"!

  43. Ozh says:

    Certes, mais, corrige moi si je me trompe, la majorité des Canadiens ne parle pas français, or les données d'après l'IP ne permettent pas d'etre précis au point de localiser entre Canada/Hors Quebec, ou Quebec. Donc statistiquement, dire à un Canadien "you *may* not speak french" est plus correct que de dire "vous etes probablement quebecquois, crisse de tabernac'".
    Et puis rien ne t'empeche de reprendre l'exemple sur ton site mais de le modifier à ta guise, ca n'est qu'un exemple :)

  44. Andy Mason says:

    Please can you explain how I can unzip all of the flag_??.gif.gz files, without doing it manually 175 times using Winzip?

    Thanks.

  45. Ozh says:

    Hoooo, ok, my bad, sorry. Hadn't check my flags.zip and didn't notice I screwed up the archive :)
    I've uploaded the flags again, please get this correct zip.

  46. Andy Mason says:

    Wonderful – thanks for that! I will see if I can get the plugin working now.

  47. Cool flags on comments
    I've implemented a plugin for WordPress that displays a flag next to a commenter's name that symbolizes their geographic location based on their IP address. Make a comment on this post to check it out, or view an existing post that has comments atta…

  48. sherry says:

    That's a pretty groovy plugin!

  49. wserInfo v1.82. ip2nation (Per Gustafsson Consulting) for country codes and names tables3. Ozh for his IP to Nation pluginDownload jVi […]

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 ?