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. 301
    fran Spain »
    wrote, on 26/Jul/08 at 6:00 pm # :

    I have installed the plug and on my page. useronline these errors occur:

    Warning: fwrite (): supplied argument is not a valid stream resource in public_html/wp-content/plugins/wp_ozh_ip2nation.php on line 60

    Warning: fclose (): supplied argument is not a valid stream resource in public_html/wp-content/plugins/wp_ozh_ip2nation.php on line 61

    there solution?

  2. 302
    Geotag Your Classroom Blog - Show Off Wh... United States »
    pingback on 15/Aug/08 at 7:18 am # :

    [...] has created the IP to Nation WordPress plugin which guesses your visitor's geographic location based on their IP [...]

  3. 303
    vkhanh Viet Nam »
    said, on 28/Aug/08 at 8:46 am # :

    Thanks :)

  4. 304
    James Korea (South) »
    thought, on 01/Sep/08 at 4:51 pm # :

    Everything works fine except when I put my mouse over the flag, all I get is "your flag" instead of the corresponding country name.

    I followed your instructions and code (alt="your flag"). Did I miss something?

  5. 305
    Ozh France »
    commented, on 01/Sep/08 at 6:31 pm # :

    James » If you put alt="your flag" you're going to get… "your flag" as an alt text. Pretty obvious I guess. Use wp_ozh_getCountryName instead.

  6. 306
    Gary Australia »
    commented, on 03/Sep/08 at 5:05 am # :

    Thanks Ozh! I have used a few of your plugins and they are awesome. I'm looking forward to using IP to Nation.

  7. 307
    Oliver Germany »
    wrote, on 14/Sep/08 at 2:06 am # :

    Bonjour Ozh!

    L'installation était très simple et le plugin fonctionne bien.

    Merci et salutations en France
    Oliver

  8. 308
    IP to Nation WordPress Plugin - WP Plugi... Germany »
    pingback on 25/Sep/08 at 11:05 am # :

    [...] IP to Nation WordPress Plugin von Ozh erkennt anhand der IP Adresse die Herkunft des Besuchers und zeigt die entsprechende Flagge [...]

  9. 309
    links for 2008-09-29 United States »
    pingback on 29/Sep/08 at 4:30 pm # :

    [...] IP to Nation WordPress Plugin « planetOzh (tags: geolocation worpdress plugin country ip) [...]

  10. 310
    Helio Canada »
    thought, on 11/Oct/08 at 5:42 pm # :

    Hi Ozh,

    I'm missing some thing.
    When you say "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."
    Do I need to do this within a database, i.e. "wordpress database", do I need to create a new database, if yes, with which name.

    I'm not getting country name when I use:

    1. echo "You are probably from ". wp_ozh_getCountryName() ."";

    so I' wondering if the problem is with the database.
    I tried three ways.
    1) I used "import" within wordpress database and it created 2 files "ip2nation" and "ip2nationCountries" and got following message "Import has been successfully finished, 34153 queries executed.".

    2) I created a new database "ipcountry" and used "import" and obtained same result as before.

    3) In Home I simply used SQL (mean not withing any database) and got message Import has been successfully finished, 34153 queries executed.

  11. 311
    Maarten Netherlands »
    replied, on 13/Oct/08 at 10:42 pm # :

    This plugin sounds very cool, but I would like to use it for a different goal.

    Could it be possible to adjust this plugin to make a (custom) flag to appear for each post? (just like the date).

    Ideally I would like to choose a country (like choosing a category) when I write the post and then have the flag show up, for example, besides the date. This will be very usefull on my travel-blog :)

    I do not understand PHP and MySQL enough to make this myself, but I could adapt the code if needed.

  12. 312
    Ozh France »
    wrote, on 13/Oct/08 at 11:35 pm # :

    Maarten » http://wordpress.org/extend/plugins/category-icons/ is what you want

  13. 313
    Maarten Netherlands »
    replied, on 14/Oct/08 at 12:00 am # :

    Cool thanks.

    I also found another solution:
    http://labs.dagensskiva.com/plugins/more-fields/

    Using this I can fill in the country-code in the extra field, and use this to show the correct flag.

  14. 314
    Jk Belgium »
    replied, on 05/Nov/08 at 3:54 pm # :

    Ce plugin est vraiment génial! Ce serait terrible de voir ce flag apparaître dans l'interface admin à la demande.

    Keep doing.

    Jk_

  15. 315
    Damon Malaysia »
    commented, on 06/Nov/08 at 6:23 am # :

    It sounds greate but unfortunately when I used it in my side bar the functions did not work as i needed .
    My code:
    <?php
    echo "You are probably from ". wp_ozh_getCountryName() ."";
    echo "If so, your country flag is ";
    ?>

    And the resault is something like this :

    You are probably from
    If so, your country flag is

    I,ve import sql file with successful promp .
    so could u pls help me on this matter

    Regards

  16. 316
    Arturo Italy »
    thought, on 09/Nov/08 at 9:40 pm # :

    very interesting plugin… thanks

  17. 317
    Stevey United Kingdom »
    thought, on 21/Nov/08 at 2:17 am # :

    I have come across an issue using your plugin to display flags next to commenters names.

    It works as desired to display the flags, but it is appending the country code to the beginning.

    For example nlFLAG

    I was wondering how to get it working as yours is with just the flag.

    Any help would be appreciated, please email me if you need more information or my comments.php file.

  18. 318
    Ozh France »
    wrote, on 21/Nov/08 at 9:52 am # :

    Stevey » Please follow the examples on this page. Everything is here.

  19. 319
    Stevey United Kingdom »
    wrote, on 21/Nov/08 at 11:41 am # :

    I have followed them to the letter.

  20. 320
    Ozh France »
    commented, on 21/Nov/08 at 1:07 pm # :

    Stevey » then change 1 for 0 or the other way round, trial and error, test, try.

  21. 321
    Hum United Kingdom »
    thought, on 24/Nov/08 at 9:47 pm # :

    Hi,
    I am using your plugin in one of my website happily for two years. I was trying to use this plugin to display geo-target ad using '0′ parameter and your 'French' language example, but it only echo default value. Can you please tell me if this plugin meets my requirements. I just want to display my own ad for visitors from UK and adsense for rest visitors.
    Thanks.
    Hum

  22. 322
    Ozh France »
    wrote, on 25/Nov/08 at 12:02 am # :

    Hum » it does. Tip: use my other plugin Who Sees Ads, in conjunction with ip2nation it will be perfect for geo-targetting

  23. 323
    TechNald Philippines »
    commented, on 09/Dec/08 at 6:36 pm # :

    ia there anyway to install this automatically without setting uo mysql database??? i really don't know database and i am afraid to try it.

  24. 324
    Sjoerd Netherlands »
    wrote, on 15/Dec/08 at 9:48 pm # :

    Just a quick note on how I added support for this awesome plug into WordPress 2.7.
    In the wp-includes folder there's a file called "comments-template.php". Open it and look for the following piece of code (it's on line 1226):

    1. printf(__('<cite>%s</cite> says:'), get_comment_author_link())

    Replace it with the following lines (all between php tags of course)

    1. /* ----- ADDED: OZH'S IP2NATION PLUGIN ----- */
    2.         if (function_exists('wp_ozh_getCountryCode')) {
    3.             $countrycode = wp_ozh_getCountryCode(0,$comment-&gt;comment_author_IP);
    4.             $countryflag = 'comment_author_IP) . '.png"&gt;';
    5.             }
    6.  
    7.         if (function_exists('wp_ozh_getCountryName')) {
    8.         $countryname = wp_ozh_getCountryName(0,$comment-&gt;comment_author_IP);
    9.             }
    10.  
    11.         printf(__('<cite>%s</cite> (from ' . $countryflag . ' ' . $countryname . ') says:'), get_comment_author_link())
  25. 325
    Sjoerd Netherlands »
    said, on 15/Dec/08 at 9:53 pm # :

    Oh bummer, the formatting didn't get through correctly. Anyway, most people brave enough to mess around with default WP files will be able to figure it out, I guess. :-)

  26. 326
    Jonathan Wold United States »
    wrote, on 12/Jan/09 at 1:49 am # :

    How could I use this to redirect visitors to an alternative site? For example, send US visitors to a US site who wound their way to the .com.nz version.

    Any ideas?

  27. 327
    newinvestor23 Canada »
    wrote, on 12/Jan/09 at 2:35 am # :

    I am using this, but for a website, but I am from canada, so it brings me to the *.com/ca page, but I cannot get to the regular *.com page? how can I bypass this? or add a line of code that will allow visitors to see US site from Canada? I can goto the UK site ok… I have 3 set up
    thanks
    newinvestor23@yahoo.ca

  28. 328
    Ozh France »
    wrote, on 12/Jan/09 at 9:21 am # :

    Jonathan Wold » You can test the value of wp_ozh_getCountryCode() and if it's not 'nz' then redirect to another page/site

  29. 329
    ignacio Argentina »
    replied, on 17/Feb/09 at 4:01 pm # :

    hello my friend couls you help me to do this with the tiny flags

    i dont know any thing of codes and webs

    some friend put wordpress on my host and im learning step by step

    could you give me a little tutorial for put this litle flags in my comments.

    in my web page there are comments of people from many places so i want evry body know where are this comments

    could you help me? please

    could you send me a mail?

    thanks

    and sory for my english

  30. 330
    Ozh France »
    replied, on 17/Feb/09 at 7:34 pm # :

    ignacio » Everything you need to know is explained here. Can't do better.

  31. 331
    small France »
    said, on 28/Apr/09 at 8:41 am # :

    It works perfectly! thank you!

  32. 332
    Hide Google Ads based on Geographic Loca... United States »
    pingback on 18/May/09 at 8:31 am # :

    [...] you want to hide your Google Ads from visitors from a certain country), you can install Ozh' IP to Nation plugin as well and then use something like [...]

  33. 333
    Miguel Netto Brazil »
    wrote, on 07/Jun/09 at 12:48 am # :

    Hello guys,

    Can anyone help me to use the plugin in WordPress 2.7. I want to put the code at the side of author comment but in comments.php dont have the code

  34. 334
    Miguel Netto Brazil »
    replied, on 07/Jun/09 at 12:56 am # :

    Sorry about my comments.php not showing in the other message. You can see at http://pastebin.com/f3efeaa54
    Thanks for any help

  35. 335
    defuze India »
    wrote, on 21/Jun/09 at 9:26 am # :

    kewl plugin..

    works in wp 2.8 !!

    thank you

  36. 336
    joe Indonesia »
    thought, on 10/Aug/09 at 7:04 am # :

    Hi..

    Is there any way that we can update the database automatically from ip2nation website?

    Thanks

  37. 337
    Ozh France »
    thought, on 10/Aug/09 at 7:42 pm # :

    joe » No

  38. 338
    atrix Indonesia »
    commented, on 17/Aug/09 at 6:05 pm # :

    A friend of mine recently ask mo how to install these plug-in on his website, thus i never use these type of plug-in i also come clueless.

    But hey, some correction in phpmyadmin guidelines you mention above, there suppose to point newbie to select their "wrdp" database (left link first, and then use "import" link (not the SQL, which only for expert). I had it trial on my blog, activate the plugin, put the syntax in comments.php and the flag appear.

    the plug-in itself is cool, I'll inform my friend how ease actually to install these actually. Hehe, good for you.

  39. 339
    Mimi Argentina »
    said, on 01/Oct/09 at 12:03 am # :

    Hi!

    Could you please tell me what would be the code if I want to show content for multiple countries?

    I mean
    wp_ozh_getCountryCode() == 'us'
    only checks if the user is from US, but I would want it to check for US, Uk and Au

    How?

    Thanks for your patience!!

  40. 340
    Ozh France »
    thought, on 01/Oct/09 at 10:09 am # :

    Mimi » http://php.net/elseif

  41. 341
    mira Czech Republic »
    said, on 20/Oct/09 at 4:35 pm # :

    Hi!
    Looks great! Is there any chance you'll make it as plugin for WordPress?? Thanks!

  42. 342
    mira Czech Republic »
    said, on 20/Oct/09 at 4:38 pm # :

    Oh my…sorry. That was really stupid question. I ment if is installation for WP autmatic or if you also have to install that DB. But..never mind.. I'll try it. ,)

  43. 343
    mira Czech Republic »
    commented, on 20/Oct/09 at 5:39 pm # :

    Ok,
    I installed the plugin and also copied ip2nation and ip2nationCountries into my DB. I used your example and read all the comments here. But it still writes only:

    You are probably from
    If so, your country flag is your flag

    Nothing more.
    Thanks for your help and one more times sorry for the dummy comment before.

  44. 344
    Daddy Ukraine »
    thought, on 30/Oct/09 at 10:02 am # :

    Excellent plugin !
    Big thanks.

    But not worked until I set absolute path to flags directory.
    After this little fix all works perfectlly.
    Thanks again!

  45. 345
    Jamie United Kingdom »
    thought, on 31/Oct/09 at 4:28 pm # :

    Hi mira,

    I couldn't get it working either. After some testing I found that for some reason my files aren't viewable if they have an underscore in the filename e.g. flag_us.gif

    Rename the flags too flagus.gif and try it – worked for me anyway!

  46. 346
    km603 Canada »
    thought, on 22/Nov/09 at 5:04 am # :

    Hi,

    I am also having the same problem as other users.

    You are probably from
    If so, your country flag is your flag

    Basically, it doesn't return which country a user is from.

    I have imported the sql already.

    Can any body tell me what is wrong? Thanks.

  47. 347
    km603 Canada »
    replied, on 22/Nov/09 at 6:34 am # :

    but when i tried

    It works as expected. Looks like the script is not getting the ip address atomatically?

  48. 348
    km603 Canada »
    said, on 22/Nov/09 at 6:59 am # :

    Looks like its not script problem.

    When i do wp_ozh_getCountryName(0,"24.83.20.XXX") my ip adress, it doesnt return any thing. looks like the database doesnt have my ip record?

    But my ip is a canadian ip, so be common. and also on this site and ip-to-nation site, they show me ip and country correctly.

    I have no idea what is wrong.

  49. 349
    hong United States »
    wrote, on 29/Dec/09 at 9:07 pm # :

    I had the same problem with

    You are probably from
    If so, your country flag is your flag

    I cleaned out the cache (wp-content/cache/ip2nation) and it's working now.

    Hope this helps.

  50. 350
    Hikari Brazil »
    replied, on 24/Jan/10 at 5:39 pm # :

    Flags.zip is not available anymore, could you host it again?

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 ?