In: , , ,
On: 2004 / 08 / 27 Viewed: 212795 times
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

    Pages: [1] 2 3 4 5 6 7 8 » Show All

  1. 1
    pinksocks.co.uk » an exceedingly p... United States »
    pingback on 27/Aug/04 at 8:53 pm # :

    [...] Categories: Asides WordPress

    Very cool new plugin from Ozh that guesses your visitor’s Country from his IP. He's looking for te [...]

  2. 2
    Nocturnal Pulse - Fun! United States »
    pingback on 29/Aug/04 at 12:01 pm # :

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

  3. 3
    Weblog Tools Collection » IP to Na... United States »
    pingback on 30/Aug/04 at 2:20 pm # :

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

  4. 4
    Blogging Pro - Blog News, Tools and Hack... United States »
    pingback on 31/Aug/04 at 3:19 pm # :

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

  5. 5
    tono Indonesia »
    wrote, on 02/Sep/04 at 3:44 am # :

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

  6. 6
    Anomalia grafica » Quando non si h... United States »
    pingback on 02/Sep/04 at 11:39 pm # :

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

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

  7. 7
    Ozh France »
    replied, on 03/Sep/04 at 2:07 pm # :

    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

  8. 8
    dario Italy »
    wrote, on 04/Sep/04 at 12:03 am # :

    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?

  9. 9
    the shaky kaiser » comment flags United States »
    pingback on 05/Sep/04 at 5:52 am # :

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

  10. 10
    shaky Hong Kong »
    wrote, on 05/Sep/04 at 7:58 am # :

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

  11. 11
    Sara United States »
    commented, on 05/Sep/04 at 2:43 pm # :

    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. :)

  12. 12
    Ozh France »
    replied, on 05/Sep/04 at 3:20 pm # :

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

  13. 13
    shaky Hong Kong »
    wrote, on 06/Sep/04 at 5:52 pm # :

    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.

  14. 14
    Jen United States »
    wrote, on 06/Sep/04 at 6:29 pm # :

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

  15. 15
    Ozh France »
    thought, on 06/Sep/04 at 6:40 pm # :

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

  16. 16
    Jean-Pascal Canada »
    said, on 07/Sep/04 at 2:23 am # :

    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"!

  17. 17
    Ozh France »
    said, on 07/Sep/04 at 5:26 pm # :

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

  18. 18
    Andy Mason United States »
    wrote, on 12/Sep/04 at 5:55 am # :

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

    Thanks.

  19. 19
    Ozh France »
    said, on 12/Sep/04 at 2:04 pm # :

    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.

  20. 20
    Andy Mason United States »
    said, on 12/Sep/04 at 5:23 pm # :

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

  21. 21
    As If By Magic United States »
    trackback on 14/Sep/04 at 1:06 pm # :

    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…

  22. 22
    R@lf Netherlands »
    replied, on 20/Sep/04 at 3:09 pm # :

    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?

  23. 23
    Ozh France »
    wrote, on 20/Sep/04 at 4:36 pm # :

    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

  24. 24
    w8in United States »
    thought, on 26/Sep/04 at 7:23 pm # :

    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 !

  25. 25
    Ozh France »
    thought, on 26/Sep/04 at 7:33 pm # :

    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">

  26. 26
    w8in United States »
    replied, on 26/Sep/04 at 9:22 pm # :

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

  27. 27
    Ozh France »
    commented, on 26/Sep/04 at 11:16 pm # :

    no problem :)

  28. 28
    Ged Poland »
    replied, on 29/Sep/04 at 8:54 am # :

    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?

  29. 29
    Ged Poland »
    wrote, on 29/Sep/04 at 11:33 am # :

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

  30. 30
    Ged Poland »
    thought, on 29/Sep/04 at 11:40 am # :

    ok, sorry for panic,
    I have managed it

  31. 31
    Ozh France »
    commented, on 29/Sep/04 at 5:09 pm # :

    Cool :)

  32. 32
    dr.gs French Polynesia »
    thought, on 06/Oct/04 at 10:41 pm # :

    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!

  33. 33
    dr.gs Dominican Republic »
    wrote, on 06/Oct/04 at 10:58 pm # :

    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="< ?php wp_ozh_getCountryName(1,$comment->comment_author_IP) ?>">

  34. 34
    Nurudin Jauhari Indonesia »
    thought, on 08/Oct/04 at 12:28 pm # :

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

  35. 35
    Ozh France »
    said, on 08/Oct/04 at 5:39 pm # :

    ??
    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.

  36. 36
    novalis Spain »
    thought, on 21/Oct/04 at 8:29 pm # :

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

  37. 37
    Ozh France »
    thought, on 21/Oct/04 at 11:19 pm # :

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

  38. 38
    novalis Spain »
    commented, on 22/Oct/04 at 7:54 pm # :

    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?

  39. 39
    novalis Spain »
    commented, on 22/Oct/04 at 8:03 pm # :

    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

  40. 40
    Ozh France »
    commented, on 22/Oct/04 at 9:05 pm # :

    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.

  41. 41
    Frk Germany »
    wrote, on 24/Oct/04 at 1:36 am # :

    It seems really cool!!!

  42. 42
    novalis Spain »
    commented, on 24/Oct/04 at 6:44 pm # :

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

  43. 43
    novalis Spain »
    wrote, on 25/Oct/04 at 10:31 am # :

    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

  44. 44
    Jauhari Indonesia »
    commented, on 27/Oct/04 at 8:27 am # :

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

    ^_^

  45. 45
    Ozh France »
    wrote, on 27/Oct/04 at 3:12 pm # :

    thx :)

  46. 46
    novalis Spain »
    commented, on 02/Nov/04 at 1:46 pm # :

    you don't have the flag of Chile

  47. 47
    KarlanKas Spain »
    wrote, on 24/Nov/04 at 2:10 am # :

    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é

  48. 48
    Ozh France »
    said, on 24/Nov/04 at 2:36 am # :

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

  49. 49
    sherry Canada »
    wrote, on 09/Jan/05 at 5:17 am # :

    That's a pretty groovy plugin!

  50. 50
    Untitled ... by Joe Chung &raquo; jV... United States »
    pingback on 12/Jan/05 at 7:10 pm # :

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

Pages: [1] 2 3 4 5 6 7 8 » Show All

Leave a Reply

Comment Guidelines or Die

  • HTML: You can use these tags: <a href=""> <em> <i> <b> <strong> <blockquote>
  • Posting code: Post raw code (no <> &lt; etc) within appropriate tags : [php][/php], [css][/css], [html][/html], [js][/js], [sql][/sql], [xml][/xml], or generic [code][code]
  • Gravatars: Curious about the little images next to each commenter's name ? Go to Gravatar.
  • Spam: Various spam plugins on patrol. I'll put pins in a Voodoo doll if you spam me.
  • I will mark as Spam test comments, all comments with SEO names (ie "My Cool Online Shop" instead of "Joe") or containing forum-like signatures.

Read more ?