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

    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. […] has created the IP to Nation WordPress plugin which guesses your visitor's geographic location based on their IP […]

  3. vkhanh says:

    Thanks :)

  4. James says:

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

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

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

  7. Oliver says:

    Bonjour Ozh!

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

    Merci et salutations en France
    Oliver

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

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

  10. Helio says:

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

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

    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.

  13. Jk says:

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

    Keep doing.

    Jk_

  14. Damon says:

    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

  15. Arturo says:

    very interesting plugin… thanks

  16. Stevey says:

    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.

  17. Ozh says:

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

  18. Stevey says:

    I have followed them to the letter.

  19. Ozh says:

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

  20. Hum says:

    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

  21. Ozh says:

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

  22. TechNald says:

    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.

  23. Sjoerd says:

    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())
  24. Sjoerd says:

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

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

  26. newinvestor23 says:

    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

  27. Ozh says:

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

  28. ignacio says:

    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

  29. Ozh says:

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

  30. small says:

    It works perfectly! thank you!

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

  32. Miguel Netto says:

    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

  33. Miguel Netto says:

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

  34. defuze says:

    kewl plugin..

    works in wp 2.8 !!

    thank you

  35. joe says:

    Hi..

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

    Thanks

  36. Ozh says:

    joe » No

  37. atrix says:

    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.

  38. Mimi says:

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

  39. mira says:

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

  40. mira says:

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

  41. mira says:

    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.

  42. Daddy says:

    Excellent plugin !
    Big thanks.

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

  43. Jamie says:

    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!

  44. km603 says:

    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.

  45. km603 says:

    but when i tried

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

  46. km603 says:

    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.

  47. hong says:

    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.

  48. Hikari says:

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

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 ?