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. […] IP to Nation WordPress Plugin Show flags of countries of commentors on your blog or website (tags: wordpress plugins blogs) –> […]

  2. webmaster says:

    Where can I find all these flags?

    Do you have the catalan flag? The flag of Catalonia?

  3. Jesus says:

    I was testing your plugin, am i had problem to validate the XHTML of my blog, it give me a message of error because the code you give here, doesn't close the img tag.

    So, i use this and now it works perfect, i have my Valid XHTML 1.0 Transitional message!

    "; ? >

    Peace bro.

  4. Jesus says:

    Ups, the code is this

    echo "";

    Of course, with the php tags

    And, i'm not from Chile, i'm from Venezuela, in the newer version of the IP2Nation SQL data base it's ok.

  5. Ivan Minic says:

    Those small flags are lookin good!

  6. L@vuk says:

    what a great plugin!
    and still attracting the same level of interet (by the way it's now available from the WP Plugin site (ip-to-country's data Version: 2.0 Author: Priyadi Iman Nurcahyo Author URI: http://priyadi.net/ */ /* adapted from the ip to nation plugin from http://planetozh.com/blog//archives/2004/08/27/ip-to-nation-plugin/ */)

  7. […] – Bad Behavior Well known plugin to prevent spam and keep your blog clean. – Adsense-Deluxe Easily add adsense to your post or anywhere inside your blog. – Comment Live Preview See live preview of your comments before you post your comment. Erm..not live enough though.. – Quote Comment Enable anyone to reply to anyone else's comment to the posts. Useful.. Erm., But untill now I haven't use it yet… – Cricket Moods Insert moods in your posts. Fun.. – Custom smileys Customize smileys at your blog. Insert them to your post or comment. Upload and specify your own shortcuts for them., Very easy to manage. God D*mn good. Suspek r u…er..Lazy N Useless… – Gravatar & Gravatar Signup Thay are for Gravatar user to display their gravatar at their comments. Very cool., See my gravatar, OMGWTF?!! If you dnot have gravatar, instant signup at the comment area. – Headline Images Turn your post title into graphic-text magically., You can use your own font too.. – PHP Exec For advanced user to run PHP script into their posts. – SimpleTags Put technorati's tag with ease. Just put tag in between [tags][/ tags] tag.. e.g. [tags]tags, tag, tags, [/ tags]……. – IP 2 Nation Detects where your visitor from and view or display their origin or IP. You can even use this what so called awesome tiny flags pack. You can see tiny flag under the gravatar at post comments at my blog. kewl huh.. […]

  8. scart says:

    hello i've got this error…

    WordPress database error: [Table 'scarty_wordpress.ip2nationCountries' doesn't exist]
    SELECT c.country, c.code FROM ip2nationCountries c, ip2nation i WHERE i.ip

  9. Ozh says:

    scart » why not read the first paragraph of this very page ? You obviously didn't create the mysql tables.

  10. scart says:

    sorry for code i've posted. my problem now. i will ask i can get the icon flag.

  11. scart says:

    sorry for all the comments. what if i put the icon beside the country name? what is the snippets?

  12. scart says:

    sorry for the panic i already managed it =) cool plugins its really nice…

  13. […] IP 2 Nation1.1 Click Counter1.01 […]

  14. Nany says:

    Thx for this script. ^^
    I loved!!!
    Congratulations!!!!
    Kisses

  15. jawhn says:

    neat, two for the price of one (or none, as the case may be :) thanks again!!

  16. jawhn says:

    ok so I guess I'm the only retarded one who can't make it work…

    flags placed, sql dbs updated and I'm using your very last example (I just wanna place the flag next to poster names, that's all) but it doesn't seem to be pulling the flag data.. instead it just places the img src=flag_ (ie. no CC.gif at the end) ideas?? I'm sure it's something stupid but I dunno what :(

    Danke!

  17. […] Έβαλε ο cosmix (εδώ και κάποιο καιρό) και ζήλεψα. Το plugin (για WordPress) μπορείτε να βρείτε στο french frag factory. […]

  18. […] 3. Plugin 설치 목록    ♪  ImageManager    ♪  Jerome's Keywords    ♪  Lightbox Plugin    ♪  WP-Stats    ♪  WP-ShortStat(Ajax)    ♪  Category Visibility    ♪  WP-Amazon (add: April 1, 2006)    ♪  WP-Amazon (add: April 1, 2006)    ♪  IP 2 Nation (add: April 2, 2006) […]

  19. · ip says:

    […] You are probably from FranceIf so, your country flag is http://planetozh.com/blog//archives/2004/08/27/ip-to-nation-plugin/ […]

  20. […] I'd appreciate your leaving a comment on the plugin page, to suggest any improvement, bug fix, or just to say if you like the plugin or not By the way, you'll find on my site a few other plugins (IP to Nation plugin in particular) you may find of interest. […]

  21. Joe says:

    Hi,
    I'm having a little problem with the example code you have for adding it to the comments, it works the image shows up but I get text with the image like : "us[IMG=flag_us.gif] (Note : the [IMG ] would be the real image.) I checked and I'm using the exact code from your example but the extra text still does not go away. How to I get rid of the extra text ? An example can be found at : http://blog.fileville.net/?p=133#comments Thanks…

  22. Ozh says:

    Joe » well, you obviously don't use the same code. Otherwise, it would work the same, right ?

  23. Joe says:

    I compared the exact code from the example and the code that I'm using there almost exactly the same except for one thing : instead of "/images/" I have it as "/flags/".
    Other then that there the same. Thanks…

  24. dutch_cze says:

    hi, nice work, i love it :)

    thank you for this script ;)

  25. res says:

    Hey I have some problems with 2.0.2, i've upgraded from 2.0 and the plugin stopped working, no errors, just empty space after /from/, some comments have Cyprus as the region which is very strange.

  26. Chris says:

    Awesome plugin! Have it implemented on my site. There's just one small problem, but no fault of yours…

    There is a 'gb' country code – Great Britain (UK) – which doens't have a flag, so I just made a copy of flag_uk.gif and named it appropriately

  27. Anna says:

    Like what Jewel said, the UK flag is not appearing at all :( I followed all your instructions here. How do I get rid of the country name *us, my etc* ? Thank you.

  28. James says:

    Hi — I've copied the code to have the little flags appear next to the commentators name, and the image doesn't show up. I've uploaded the flags. Would the Regulus theme mess with the code? Here's what I have but it's not working:

    ">
    "> comment_author_IP)
    . '.gif">' ;
    ?> –

    it only shows up as "us commentator"

  29. Teresa says:

    Great plugin. Using it with the famfamfam flags :) Thanks + well done!

  30. Jenny says:

    Great plugin! :)

  31. Anthony says:

    great plugin :)

    good work

  32. Doufer says:

    Nice Plugin!

  33. Omry Yadan says:

    Hi, I tried your plugin, but something is wrong.
    I imported the database table, but for some reason the wp_ozh_getCountryCode() function always returns "01" for me.
    any idea why?

  34. This seems like a very cool plugin!
    I am a plugin freak and will be using this soponer or later! Thanks!

  35. dave says:

    yes lets make it work.

  36. x2nie says:

    muahahahaaha :D it's great!
    tons of country!? hey, your sql is a hardwork well done ya?! hmmm, whatever greeting, :thanks you very much. :!!:
    (i'am now trying to plug it on my own newest site)

    :top!

  37. wesley says:

    Very good!!!!

  38. ozmex says:

    hum, almost cool, was expecting a mexican flag and i got a german flag (post 189)

  39. Seba says:

    Hi!

    Very nice plugin! I use it at my website.
    Goode job ;)

  40. Jenny says:

    this is a cool plugin. i just got it to work after like 2 hours. LOL. i love it tho.

  41. Iva says:

    Let's see how it works…

  42. Joe says:

    Hi,
    I have finally figured out why it would display the country code and the flag, I guess that 1 prints out the country code, but when your also echoing it then you get double. :-)

  43. […] All of my plugins work. They include Akismet, Bad Behavior, Angsuman's Feed Copyrighter, Counterize, Get Recent Comments, WP-Polls, Simple Tags, Subscribe To Comments, todayAgo, WP Grins, WP-IPAT, and IP 2 Nation. […]

  44. […] WordPress IP 2 Country-City is another plugin adapted from IP-to-Nation, IP-to-Country and GeoIP plugin. The major difference is that this plugin shows not only the Country Name/Flag but the City Name according to Maximind's GeoLiteCity database. […]

  45. […] IP to Nation Plugin – IPë¡œ 게시자 나라의 국기를 표시해 주는 플러그 인. (pionelle님의 한글 메뉴얼도 있음) […]

  46. […] WordPress IP 2 Country-City is another plugin adapted from IP-to-Nation, IP-to-Country  and GeoIP plugin. The major difference is that this plugin shows not only the Country Name/Flag but the City Name according to Maximind's GeoLiteCity database.  Maximind says that the GeoLiteCity database has 97% of accuracy for Country and 60% for cities in US (there are some cities from other countries), but you have always an option to buy the full version at Maximind which is more complete.  Requirements  I have only tested on WordPress 2.05. Maybe it works on previous versions. If you tested on a previous version, post a comment and I'll change this text.  Installation […]

  47. […] you have to get a plugin called IP to Nation (IP2Nation) Plugin from PlanetOzh. Do note that you have to also get the MySQL table from […]

  48. Jan Rune says:

    This is great! Thanks mate

  49. Sai says:

    Testing this plugin

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 ?