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

    Hey, thanks a lot for this gr8 plugin, I've already got it working on my site. Can I ask, how the alt tags display the country name, like on this site?

  2. Ozh says:

    Aaron » obviously replace the alt tag with PHP function wp_ozh_getCountryName()

  3. Aaron says:

    Thanks Ozh, I've made the changes and everyhing works fine n perfect!! Merci tres beaucoup! :)

  4. Iva says:

    Let's see if these flags are updated…

  5. Iva says:

    Apparently not:(

  6. […] considered that his works will never be complete and never be finished. Other plugin from France is IP-to-Nation by Ozh, which inspired Priyadi Iman Nurcahyo to create his self-made plugin. […]

  7. DarK says:

    Hello,

    Thanks for this plugin…

  8. Jenny says:

    This looks like a nifty plugin. Think I'll give it a go. Thanks for making it.

  9. 5lsd says:

    hello,
    i like wp plugin!

    i try to use it

  10. Gavnit says:

    Thanks!
    Really a nice plugin

  11. BigadicMania says:

    Test :P nice plugin

  12. Zane Dickens says:

    Hey nice plugin.

    Why allow html? I'm confused… I tried it – if it doesn't work well then I'll look like an a$$.

    Kung Fu Cabbage!

    PS Would really like to know how you did your syndicate section?

  13. Denis says:

    Did any one have http://www.ip2nation.com/ *.sql file?
    I can`t get it from them. Please, help me.

    My e-mail:
    admin at deonisij dot com

  14. Kaka says:

    Hi….

    I got a funny message (after upgrade from wordpress version 2.2.3 to 2.3) in the "Plugins Management" under the IP 2 Nations section, like below :

    There is a new version of IP 2 Nation available. Download version 1.2 here.

    So, i click the link of that funny message and its bring me to http://wordpress.org/extend/plugins/ip-to-nation/ and of course i saw "Version: 1.2", so i donwload it, but the size of *zip file ONLY 1 kb, and when i extract it, i get the funny message again: Zip file is empty
    No files to process
    .

    So, can you explain to me about that?

    Blessing,
    K

  15. Ozh says:

    Kaka » sorry about the noise, there is no such version 1.2 as of today, I just got something wrong with the WordPress plugin repository. However, a version 1.2 is under works.

  16. Anna says:

    Just saying thanks for a great and fun plugin.

  17. Dizzy100 says:

    Hi. Im trying this plugin and have downloaded the latest database from ip to country but im having some issues with it.

    Its all installed in my wordpress database with database names

    ip2nation
    ip2nationcountries

    and i have you plugin installed.

    When i use the php example however and browse to the snippets location i get

    You are probably from Private

    Any ideas ?

    I was hoping to use the plugin to geotarget certain ads (amazon etc).

  18. Dizzy100 says:

    Just to add to this im getting the same results with ip to country.

    It detects other peoples IP's, i can insert various data to check servers ips to country (they always detect the right location) but mine won't detect at all.

    I wasn't sure if its my ip but i noticed your comment system has picked up my location. Any ideas ?

  19. Ozh says:

    Dizzy100 » weird. Sorry I have no idea, it's probably a wrong installation of ip2nation's SQL file. I'm planning to release an update for this plugin sometimes, I hope to implement an auto-installer, maybe it'll help.

  20. Dizzy100 says:

    Its running of my local machine so maybe thats the issue. I'll investigate further.

    Everything else is detected on either plugin so god only knows why. Its the only possible pointer to my error. Your plugin says private and ip to country doesn't display a thing (both with the newest databases). I'm wondering if its detecting my local IP address and thats the issue.

    I'll find out more when i put this baby online.

    Great work BTW, love its usage ideas :)

  21. Ozh says:

    Dizzy100 » owwwww of course, if it's running on your machine, it displays a "Private" country. What country do you expect from 127.0.0.1 or 192.168.0.1 ?

  22. Dizzy100 says:

    I was loggin in through my ip not localhost so assumed it would have handed of from the browser. DOH :) You live and learn :)

    Thanks for the replies.

  23. Dear Ozh, thanks for writing this mod, its really fancy! I have however one tiny issue. I get both the 2 letter country code + the flag image behind my posters names in the comment field. I tried several things but cant find whats going wrong. Can you please take a look? (for example http://www.samsin.nl > guestbook)

    PS: I'm using the following php code in my comments.php:

    php echo 'comment_author_IP) . '.gif">'

  24. Ozh says:

    Normen » detailed examples are available on this page including this very one.

  25. normen says:

    Dear Ozh,
    Cheers for your reply. I messed something up last night while pasting the code I am using in this comment field. However, I'm using the exact same one as shown in the examples above. To be specific "To put a tiny flag by the name (like in comments here), you could use the following code :", so that code should only display the flag without the country code like on your site. But that exact same code generates the country code + flag output. Any ideas?

  26. Edward says:

    Ozh,

    is there a way to make the output of this plugin, look like:


    Posted from [IP Address] [City] [Flag] [Country]

    if there please tell me

  27. Ozh says:

    Edward » No, obviously, there is no "city" guessing.

  28. Edward says:

    Oh sorry for that, so, once again, wanna the output like bove but without "city":

    [IP Address] [Flag] [Country Name]

    Please tell me

  29. Ozh says:

    Edward » please read this page. All the info needed are here.

  30. engel says:

    Hi,

    I'm using WordPress 2.3. I just installed your plugin, and both functions always return ". I have the correct MySQL tables installed from it.. I can't figure out why it's not returning anything.

    Thanks,
    engel

  31. jenny says:

    these plugin was sooo cool….what about the IP address? how to detect visitor's IP add?

  32. […] you would like to display country flags next to each user, then install the ip2nation plugin. You also need to manually install two mySQL database tables, and populate them with Ip data for […]

  33. Nancy says:

    Please, what do I do with .sql?
    Is the same like an other plugin?
    I put it in wp plugins together the ip2 nation?

    Thans

  34. Ozh says:

    Nancy » Everything is explained here. Read what I wrote. I cannot help more.

  35. […] not coming from these countries, you'll greatly be affected. I'm planning to install this WordPress plugin so as to show Widgetbucks ad only on U.S. and Canada. There's no point on showing their ads […]

  36. impropio says:

    gracias buen plugins!

  37. Leendert says:

    Hi,

    downloaded and uploaded your plug-in in the plugin folder. Created the tables with the provided sql file.

    I'm unable to activate your plugin: it won't show up in the list of plugins :(
    I tried placing your .php by itself (wp-content/plugins/wp_ozh_ip2nation.php) and in a folder (wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php), no difference, it won't show.

    I'm using WP 2.3.1 with the Unnamed 1.22 theme by Xu Yiyang.

    Any ideas?
    Kind regards, Leendert

  38. Ozh says:

    Leendert » You probably screwed something up when uploading (binary instead of ascii or something like this). There is no reason but your misdoing something :)

  39. Leendert says:

    Ozh,

    Probably a slip of the typewriter :)
    I don't know what made it go wrong. I copied and pasted your original .php and .txt content in the existing file.

    And there it was: bright and shiny. Thank you.

  40. craig says:

    Flag test for this interesting plug-in…

  41. […] IP to Nation WordPress Plugin « planetOzh 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. (tags: wordpress plugin adverts) […]

  42. […] This is the FIRST THOUGHT from me, if not most people when the news came out. And i think it's the MOST PRACTICAL. If you're living outside US/CA , you'd have noticed by now my WB has been replaced with Adsense on the sidebar. It can be easily done with WHO SEES ADS plugin with IP TO NATION database. […]

  43. Frank Lucas says:

    Hi. the "… awesome tiny flags pack" comes up 404 (as does the whole URL. Regards.

  44. Ozh says:

    Frank Lucas » just checked, no 404 for me…

  45. Malaga says:

    I was lookin for this plugin times ago…ive tested with 2.3.1 and there are many bugs. Once inserted the sql into db, ive activate the plugin. From now on i see a "||" on the top of admin page, and it breaks plugin like WP AJAX Edit Comments and changes the font dimension of the post.
    I hope u will fix it, its quite unusable…

  46. Malaga says:

    Ive found the problems…just remove the "|" at the top and the bottom(before the ) and all should work fine, althought work fine for me. I dont know why you put this "|" in the code…bah…

  47. stanch says:

    @edward

    in just a little while, i will released a wordpress plugin to translate commenters’s ip address to their city and country.

    so, please always monitoring my blog @ http://aufklarung.org

  48. Ozh says:

    Malaga » I don't see any "|" anywhere and it seems no one has ever seen them in the previous 15,000 downloads before you.

  49. Greetings again! wow in just 2 days I learn so much in your blog. Thank you so much for your tips and well written blog.

    So I just took a bit of time to read this page and I already inserted IP nation ( which I has running in my blog for long time hehe ), but this time on my comments section. and it works fine!!! :)

    Tell me my friend is there any way you can help me with my category list? Please? :)

    What I have now is a huge category / country list which needs to be shortened, but I would like to have something that would just basically close / open sub-categories with a simple click, maybe expandable sub-categories. Do you know of anything?

    Thank you, great blog! All the best for your family!

  50. Ozh says:

    João Leitão » I know there are a few plugins around, but didn't try any. You could give a try to this one or this one

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 ?