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. Todd McCaffrey says:

    I'm running WordPress 2.9.1 and getting the error:

    Warning: array_key_exists() expects parameter 2 to be array, null given in plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 73

    My hack around to do this:

    if ( isset($wp_ozh_ip2nation['results']))
    {
    if ( !array_key_exists($ip, wp_ozh_ip2nation['results']))
    $wp_ozh_ip2nation['results'][$ip] = wp_ozh_ip2nation($ip);
    }
    else
    {
    $wp_ozh_ip2nation['results'][$ip] = wp_ozh_ip2nation($ip);
    }

    Ugly, I know! (sorry about the formatting)

  2. Ozh says:

    Hikari » fixed, get it here

  3. MonkeyMan says:

    I love this plugin!

  4. Anne says:

    Hallo Ozh. I love these little country flags. You know I am going to attempt to install this plugin of yours this weekend. I just wish I had more comments, it would look then much nicer :)

    Warm regards to you and to all yours.

  5. Jared says:

    I like the idea of this plugin, but I am having a hard time getting it to work with WP v2.9.2. PHPMyAdmin is installed on my server by my web host and I use it to manage databases for several domains. I have tried both importing the sql data to my wp database and creating a completely new one populated by the imported sql. I then installed your plugin via wordpess admin and activated it. I could not find any configuration option for the plugin, so next I just added the following code based on your example to my theme's sidebar to see what would happen:

    <?php
    echo "You are probably from ". wp_ozh_getCountryName() ."";
    echo "If so, your country flag is ";
    ?>

    All I end up with is "You are probably from" and "If so, your country flag is" without the country or flag displayed. I figure the problem is most likely database related, but I do not know how to fix it and without some kind of config option don't know where to start.

    On another note, I think this plugin would make a great widget.

  6. Hikari says:

    Jared, probably you didn't imported the sql correctly, and tables are not being found. This plugin doesn't have admin page as I remember, you just need to have the ip2nation tables and use its functions.

    Take a look on my plugin Hikari Enhanced Comments, it's based on this one and allows you to have ip2nation tables in a unique database and share it among all WP sites. Of course you must config database user access and so on, but it may help you.

  7. Mike says:

    Hey, I have exactly the same problem as Jared above me.

    Is this because i use wp 2.9.2 or should we install those two tables in a specific database.

    I looked through the plugin and can't figure out where you call the database, thus I'm assuming you're working on the current database, which should be the one for the WP. Still, I can't really make it work.

    Any help would be greatly appreciated. Thanks

  8. Mike says:

    Ok, I've come with updates. It seems I've installed correctly the database, thanks Hikari for your post ;)

    Anyway, i still can't make some of the queries work.

    For instance, i get results for

    But i get nothing for:

    And this is the function I would need so i could get the name of the country where my visitors comes from. Neither does wp_ozh_getCountryCode() work.

    Any help here would be greatly appreciated…

  9. justblog says:

    I cant use this plugin :(

  10. joe says:

    I have installed "WP-ViperGB" plugin which it needs your plugin "IP to Nation" to show the contries' flags.

    I have followed all steps in your blogs and the plugin works fine. However, I have one problem: a specific country's flag don't show and instead, it shows a broken image.

    Any thoughts please?

  11. John Rees says:

    Thanks Ozh for this plugin.

    I have got it working easily on a number of sites now.

    John

  12. janvier says:

    Isn't this plugin the WP-IP2Nation-Installer one ? The SQL is exactly the same,… which one is the original ?

  13. Ozh says:

    janvier » Don't know this plugin and chances are the SQL is the same because it comes from http://www.ip2nation.com/ For the record this plugin of mine was made more than 6 years ago…

  14. cutesitedeveloper says:

    Hi have viewed your really nice preparation.

    My Queries:

    Have installed your cute plugin and execute the .sql file also. I don't know any comment option, But i need redirect my website link based country like

    http://www.blog.com/en/
    http://www.blog.com/ar/
    http://www.blog.com/fr/

    I got a code from ip2nation.com via, they provide redirect code. I am using wordpress website manner so now i am in paste that ip2nation code from where?

    Last time have tried to theme – header.php but they take a more than time, But result not done.

    Please help me what i do, or give any solution thanx

  15. Craig says:

    I know the plug-in is over half a decade old, but it looks like the decimal network comparison is failing when the ip address matches exactly with the lowest range of ip block.

    i.ip < INET_ATON('".$ip."')

    should be

    i.ip <= INET_ATON('".$ip."')

    …otherwise 3.0.0.0 (50331648 in decimal) shows Sweden instead of USA.

  16. katoey says:

    also this, will this still work with the present version of wordpress? and possibly the next updates too??ty

  17. Ozh says:

    katoey » the plugin page and its readme say it all

  18. Vicente Ruiz says:

    I can use yor plugin functions as a libary (and distribute it) for my own plugin?

  19. Ozh says:

    Vicente Ruiz » sure

  20. Jerome says:
    1. "I had the same problem with
    2.  
    3. You are probably from
    4. If so, your country flag is your flag
    5.  
    6. I cleaned out the cache (wp-content/cache/ip2nation) and it's working now.
    7.  
    8. Hope this helps."

    Yes Mister Hong (comment 349), it was the solution !!

  21. Pablo Sosa says:

    Very cool plugin mate, well done. Cheers for taking the time to put it up.

  22. Pablo Sosa says:

    Ozh what do you suggest as a fix when changing the file upload size is restricted (changing it it's not an option on this server) but the IPNATION sql file is bigger than the server allows? Can it be run from somewhere else. I really want to use your option, it's such a time saver for me. Any help would be tremendously appreciated.

  23. Ozh says:

    Pablo Sosa » your host wont let you upload a 3 MB file? The fix I suggest is to quit your crap host.

  24. Pablo Sosa says:

    Ok, cheers Ozh, Changed host, full root rights on this one, fully geared up.
    Now.

    I am using:
    http://wordpress.org/extend/plugins/wp-ip2nation-installer/
    is this the same plugin from you?

    It works like a CHARM with countryname…but I can't seem to figure out how to insert the awesome little flag

    // Output full country name
    echo $countryName;

    and then? Please help! I sooo love this pluing.

  25. Ozh says:

    Pablo Sosa » I don't know this plugin.

  26. Anthony says:

    I am trying to get the flags to show. When I inserted the code I received an error so I removed it. In reading your instructions and I see I am to add the script when I see , etc. I am using 2011 theme and do not see this anywhere in the blogroot/wp-content/themes/yourtheme/comments.php. Could you tell me where the code goes in this instance.

    I thank you in advance for your assistance.

  27. David says:

    3 years no posts, just wanted to say, still uding your plugin, and it rocks! :-)

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 ?