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. 201
    Aaron United Kingdom »
    wrote, on 01/Jul/07 at 5:43 pm # :

    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. 202
    Ozh France »
    thought, on 01/Jul/07 at 6:13 pm # :

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

  3. 203
    Aaron United Kingdom »
    said, on 01/Jul/07 at 10:16 pm # :

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

  4. 204
    Iva Republic of Serbia »
    said, on 22/Jul/07 at 12:22 am # :

    Let's see if these flags are updated…

  5. 205
    Iva Republic of Serbia »
    commented, on 22/Jul/07 at 12:22 am # :

    Apparently not:(

  6. 206
    The World, Plugged In: Various WordPress... United States »
    pingback on 31/Jul/07 at 2:39 pm # :

    [...] 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. 207
    DarK Turkey »
    replied, on 17/Aug/07 at 9:39 am # :

    Hello,

    Thanks for this plugin…

  8. 208
    Jenny United States »
    said, on 17/Aug/07 at 6:23 pm # :

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

  9. 209
    5lsd China »
    commented, on 22/Aug/07 at 3:04 pm # :

    hello,
    i like wp plugin!

    i try to use it

  10. 210
    Gavnit India »
    replied, on 30/Aug/07 at 6:13 am # :

    Thanks!
    Really a nice plugin

  11. 211
    BigadicMania Turkey »
    said, on 02/Sep/07 at 2:32 am # :

    Test :P nice plugin

  12. 212
    Zane Dickens South Africa »
    commented, on 06/Sep/07 at 3:54 pm # :

    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. 213
    Denis Lithuania »
    replied, on 14/Sep/07 at 9:28 am # :

    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. 214
    Kaka United States »
    thought, on 26/Sep/07 at 5:23 pm # :

    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. 215
    Ozh France »
    said, on 26/Sep/07 at 8:29 pm # :

    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. 216
    Anna Sweden »
    wrote, on 28/Sep/07 at 9:33 am # :

    Just saying thanks for a great and fun plugin.

  17. 217
    Dizzy100 United Kingdom »
    commented, on 01/Oct/07 at 12:37 pm # :

    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. 218
    Dizzy100 United Kingdom »
    said, on 01/Oct/07 at 1:14 pm # :

    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. 219
    Ozh France »
    said, on 01/Oct/07 at 6:35 pm # :

    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. 220
    Dizzy100 United Kingdom »
    said, on 01/Oct/07 at 7:25 pm # :

    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. 221
    Ozh France »
    replied, on 01/Oct/07 at 10:40 pm # :

    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. 222
    Dizzy100 United Kingdom »
    replied, on 02/Oct/07 at 12:17 am # :

    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. 223
    Normen Sam-Sin Netherlands »
    said, on 02/Oct/07 at 2:54 am # :

    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. 224
    Ozh France »
    wrote, on 02/Oct/07 at 1:39 pm # :

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

  25. 225
    normen Netherlands »
    thought, on 02/Oct/07 at 8:34 pm # :

    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. 226
    Edward United States »
    commented, on 04/Oct/07 at 11:18 pm # :

    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. 227
    Ozh France »
    commented, on 05/Oct/07 at 8:59 am # :

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

  28. 228
    Edward United States »
    thought, on 06/Oct/07 at 5:23 pm # :

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

    [IP Address] [Flag] [Country Name]

    Please tell me

  29. 229
    Ozh France »
    commented, on 07/Oct/07 at 6:09 pm # :

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

  30. 230
    engel United States »
    said, on 10/Oct/07 at 10:19 pm # :

    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. 231
    jenny Philippines »
    commented, on 17/Oct/07 at 4:50 am # :

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

  32. 232
    [WORDPRESS] wp-useronline hack | Cape To... United States »
    pingback on 22/Oct/07 at 7:11 pm # :

    [...] 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. 233
    Nancy Brazil »
    commented, on 02/Nov/07 at 4:18 am # :

    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. 234
    Ozh France »
    said, on 02/Nov/07 at 9:10 am # :

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

  35. 235
    Bad News For Widgetbucks Publishers - Ma... United States »
    pingback on 15/Nov/07 at 4:21 am # :

    [...] 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. 236
    impropio Venezuela »
    said, on 15/Nov/07 at 8:35 pm # :

    gracias buen plugins!

  37. 237
    Leendert Netherlands »
    commented, on 16/Nov/07 at 10:58 am # :

    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. 238
    Ozh France »
    said, on 16/Nov/07 at 11:40 am # :

    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. 239
    Leendert Netherlands »
    replied, on 16/Nov/07 at 1:47 pm # :

    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. 240
    craig Peru »
    replied, on 17/Nov/07 at 8:37 pm # :

    Flag test for this interesting plug-in…

  41. 241
    links for 2007-11-18 United States »
    pingback on 18/Nov/07 at 3:25 am # :

    [...] 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. 242
    Make Money: MALAYSIA! » Blog Archi... United States »
    pingback on 23/Nov/07 at 10:33 am # :

    [...] 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. 243
    Frank Lucas United States »
    wrote, on 08/Dec/07 at 9:23 pm # :

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

  44. 244
    Ozh France »
    said, on 08/Dec/07 at 9:57 pm # :

    Frank Lucas » just checked, no 404 for me…

  45. 245
    Malaga Italy »
    said, on 17/Dec/07 at 3:51 am # :

    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. 246
    Malaga Italy »
    said, on 17/Dec/07 at 2:33 pm # :

    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. 247
    stanch Indonesia »
    commented, on 18/Dec/07 at 1:32 pm # :

    @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. 248
    Ozh France »
    replied, on 18/Dec/07 at 10:29 pm # :

    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. 249
    João Leitão Morocco »
    said, on 08/Jan/08 at 10:54 am # :

    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. 250
    Ozh France »
    wrote, on 08/Jan/08 at 8:47 pm # :

    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

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 ?