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. 351
    Todd McCaffrey United States »
    thought, on 25/Jan/10 at 7:10 pm # :

    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. 352
    Ozh France »
    commented, on 25/Jan/10 at 10:06 pm # :

    Hikari » fixed, get it here

  3. 353
    MonkeyMan Bahrain »
    replied, on 05/Feb/10 at 1:16 am # :

    I love this plugin!

  4. 354
    Anne Australia »
    thought, on 19/Feb/10 at 10:33 am # :

    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. 355
    Jared United States »
    replied, on 05/Apr/10 at 9:08 pm # :

    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. 356
    Hikari Brazil »
    wrote, on 05/Apr/10 at 10:57 pm # :

    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. 357
    Mike Romania »
    wrote, on 13/Apr/10 at 5:34 pm # :

    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. 358
    Mike Romania »
    said, on 13/Apr/10 at 6:24 pm # :

    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. 359
    justblog Indonesia »
    said, on 16/Apr/10 at 7:41 pm # :

    I cant use this plugin :(

  10. 360
    joe Kuwait »
    wrote, on 21/Nov/10 at 6:03 pm # :

    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. 361
    John Rees United Kingdom »
    replied, on 23/Nov/10 at 6:53 pm # :

    Thanks Ozh for this plugin.

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

    John

  12. 362
    janvier Canada »
    said, on 24/Nov/10 at 2:54 pm # :

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

  13. 363
    Ozh France »
    commented, on 24/Nov/10 at 3:13 pm # :

    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. 364
    cutesitedeveloper Saudi Arabia »
    wrote, on 05/Dec/10 at 4:18 pm # :

    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. 365
    Craig Peru »
    replied, on 30/Apr/11 at 10:54 pm # :

    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. 366
    katoey Philippines »
    commented, on 30/Jul/11 at 8:29 pm # :

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

  17. 367
    Ozh France »
    replied, on 01/Aug/11 at 7:47 am # :

    katoey » the plugin page and its readme say it all

  18. 368
    Vicente Ruiz Spain »
    thought, on 22/Aug/11 at 3:24 am # :

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

  19. 369
    Ozh France »
    thought, on 22/Aug/11 at 8:46 am # :

    Vicente Ruiz » sure

  20. 370
    Jerome Belgium »
    wrote, on 10/Oct/11 at 12:25 pm # :
    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. 371
    Pablo Sosa Panama »
    said, on 10/Nov/11 at 7:49 pm # :

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

  22. 372
    Pablo Sosa Panama »
    wrote, on 10/Nov/11 at 9:50 pm # :

    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. 373
    Ozh France »
    replied, on 10/Nov/11 at 10:25 pm # :

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

  24. 374
    Pablo Sosa Panama »
    wrote, on 13/Nov/11 at 8:15 am # :

    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. 375
    Ozh France »
    replied, on 15/Nov/11 at 1:17 pm # :

    Pablo Sosa » I don't know this plugin.

  26. 376
    Anthony United States »
    replied, on 20/Dec/11 at 8:29 pm # :

    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. 377
    David Portugal »
    thought, on 08/Apr/14 at 6:10 pm # :

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

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 ?