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. 101
    My Other Side of the Stories » Wor... United States »
    pingback on 08/Apr/05 at 4:38 am # :

    […] 7;s Browser Sniff plugin to detect visitor's browser and OS. Installing Ozh's Ip united.com">2 Nation plugin to det […]

  2. 102
    My Other Side of the Stories » Wor... United States »
    pingback on 17/Apr/05 at 5:12 am # :

    […] post. (9/10) WP Plugin Manager, useful, especially when it came to my lazy times (8/10) IP2Nation, look useful It shows wrong nation flag sometime (7/10) […]

  3. 103
    Mary Brazil »
    commented, on 02/May/05 at 9:56 pm # :

    Love your script.

  4. 104
    Ingoal’s Insight… » Pl... United States »
    pingback on 12/May/05 at 5:55 pm # :

    […] ; Ingoal @ 18:55

    Today I took the time and installed the following plugins: IP to nation which will show a country flag – guessing the origin of th […]

  5. 105
    empdesign » Blog Archive »... Argentina »
    pingback on 16/May/05 at 5:42 pm # :

    […] plicar o no los filtros del CMS antes de mostrarse. IP to Country: Versión del plugin […]

  6. 106
    day by day » Blog Archive &raqu... United States »
    pingback on 20/May/05 at 2:13 am # :

    […] ravatar di comment .. diajarin ibu ini 2. AVATAR POP UP di post .. ini lihat pluginnya di Ozh       ini contohnya : Me 3. Smilies di COmment .. li […]

  7. 107
    Unadulterated Arrogance Qatar »
    said, on 21/May/05 at 7:54 am # :

    Excellent plugin my friend.. I shall be using this when I move to my wordpress blog at http://www.arrogantly.com

  8. 108
    Stephen Singapore »
    said, on 22/May/05 at 12:49 am # :

    This plugin is great as it adds color to the overall site. Superb!

  9. 109
    andrew’s sandbox » Blog Arc... United States »
    pingback on 27/May/05 at 3:32 am # :

    […] ust using it to provide that random G2 Image you see on the right hand side of this page. IP 2 Nation – My latest addition to my blog. Comments now have the nati […]

  10. 110
    Blooming Town » Plugin: ipë¡œ ... Korea (South) »
    pingback on 07/Jun/05 at 6:19 am # :

    […] 표시만들기

    June 7th, 2005
    pionelle–>

    – 참고: IP to Nation WordPress Plugin by Ozh 설명히 자세하게 나와있ì […]

  11. 111
    Blooming Town » Plugin: ipë¡œ ... Korea (South) »
    pingback on 10/Jun/05 at 6:40 am # :

    […] ´ì½˜ 만들기

    June 7th, 2005
    pionelle–>

    – 참고: IP to Nation WordPress Plugin by Ozh 이 Plugin을 달면 상대방의 […]

  12. 112
    Victor alamo Hong Kong »
    commented, on 22/Jun/05 at 6:01 am # :

    Hi,

    I have installed the plugins in website , plus "activate". Apart from that, I have place the flags directory under my Blog. Still, I can not get show anything in :

    1.- Your probably from "empty"
    2.- If so, you country flag is "empty"

    I have the check the path and it is correct, but I have noticed that function can not identify which flag to display "flag_.gif"

    Couuld you please, give me some guidance ?
    I will really appreciate , i have spent 3 hours already with this plugin.

    Thanks again.

  13. 113
    Ozh France »
    thought, on 22/Jun/05 at 5:59 pm # :

    Well, first tell me what code you tried to use …

  14. 114
    Victor alamo Hong Kong »
    replied, on 23/Jun/05 at 3:26 am # :

    Hi,

    Here is the code

    Thanks in advance.

  15. 115
    Ozh France »
    replied, on 23/Jun/05 at 7:10 am # :

    These 2 lines look correct… So the problem is somewhere else in your page.

  16. 116
    Victor alamo Hong Kong »
    said, on 23/Jun/05 at 9:50 am # :

    Hi again,

    One question: did you place the code inside the comments.php? which it locates in wp-content/themes/mythemes/comments.php .

    For my case, I just copy & paste in that particulars file and add the open & close for
    Oh! – I make the plugins activate.
    That was all!.

    Thanks again.

  17. 117
    Victor alamo Hong Kong »
    said, on 23/Jun/05 at 10:11 am # :

    Hi,

    Hi again,
    All I see, it is that the function in code is being call, but it is not returning any value back to link , therefore…it just result in "/flags/flag_.gif" inside link to obtain the right flag.

    my main file is locate under plugins as you have mentioned in your website.

    Thanks again.

  18. 118
    matthewstinson.net » Smooth sailin... United States »
    pingback on 03/Jul/05 at 9:51 pm # :

    […] ncy Tooltips Flickr Gallery flickrRSS Jerome's Keywords Kramer Subscribe to Comments IP2Nation Cat2Tag There are a few plugins beyond th […]

  19. 119
    Blooming Town » Plugin: ipë¡œ ... Korea (South) »
    pingback on 10/Jul/05 at 2:39 pm # :

    […] – 참고: IP to Nation WordPress Plugin by Ozh 이 Plugin을 달면 상대방의 ip에 따라 저장되어 있는 170여개의 이미지에 따라 국가별 국기가 아이디 옆에 표시가 된다. 설명히 자세하게 나와있지만, 일단 영어이므로 쉽게 한글로 설명해보자. […]

  20. 120
    TinyGuy Sweden »
    commented, on 30/Jul/05 at 9:55 am # :

    This would be great if it worked!
    Unfortunately mySQL throw errors at me when trying to run the sql file… "Exceeded 5000 commands" or something like that :(

  21. 121
    Ozh France »
    replied, on 31/Jul/05 at 3:47 pm # :

    TinyGuy » doh… too bad for you. This seems to be a misconfiguration of your host, try to get in touch with your web admin.

  22. 122
    eticketride.org » ip2nation United States »
    pingback on 11/Aug/05 at 9:47 am # :

    […] […]

  23. 123
    abe Indonesia »
    said, on 22/Aug/05 at 6:55 pm # :

    ehmm.. Ozh..
    I've just moved to WPress!
    Well, i aim to use your plug-in(s), thanks dude! ^_^

  24. 124
    andufo Ecuador »
    wrote, on 22/Aug/05 at 10:44 pm # :

    i am using the plugin as well. i did make some modifications for it to work better with my weblog though…

    thanx

  25. 125
    Mitch Australia »
    wrote, on 04/Sep/05 at 9:27 am # :

    Cool idea! :D

  26. 126
    Onur Turkey »
    commented, on 12/Sep/05 at 3:34 pm # :

    great job dude

  27. 127
    Mandar India »
    replied, on 23/Sep/05 at 12:50 pm # :

    nice Work.

  28. 128
    Hoshi Italy »
    said, on 05/Oct/05 at 3:53 pm # :

    Very Cool script! It works very well!! :D

  29. 129
    苟世录 » Blog Archi... United States »
    pingback on 13/Oct/05 at 7:38 pm # :

    […] WordPress显示留言者地区的插件IP to Nation。 […]

  30. 130
    Rizwan Pakistan »
    commented, on 15/Oct/05 at 12:16 pm # :

    Hey man, great plugin, really appreciate all the work you must've put in. Thanks again.

  31. 131
    岁 月 æ—  声 » Blo... United States »
    pingback on 30/Oct/05 at 5:21 am # :

    […]   IP to Nation,可以把来访者的IP转为所在实际区域。 […]

  32. 132
    sharon Netherlands »
    replied, on 20/Nov/05 at 1:17 pm # :

    Someone could post script that integrate google maps api from ip2nation site.
    I miss on their site finally implementation of how it works.
    They said look to the source.Ok but where is code that read sql IP/country and then display flags on map.
    I'm confused.If it is so easy would be nice to post completly code for integrating with google maps api.

    Thanks

  33. 133
    fillupmybox Malaysia »
    commented, on 30/Nov/05 at 6:17 pm # :

    I've received this error:

    Parse error: syntax error, unexpected T_STRING, expecting ',' or ';'

    I Hope you can spare a moment and take a look at it.

    The country name showed up perfectly fine, just the image is giving me the trouble.

  34. 134
    Ozh France »
    wrote, on 30/Nov/05 at 9:04 pm # :

    when you echo "something", escape the quotes inside something

  35. 135
    fillupmybox Malaysia »
    thought, on 01/Dec/05 at 6:18 pm # :

    Sorry, I don't quite get it. I'm not much a technical savvy guy. Hope you can explain it in details.

  36. 136
    Ozh France »
    said, on 01/Dec/05 at 7:31 pm # :

    Quotes cannot be nested :
    echo "my name is "tom" and I'm cool" : wrong
    echo "my name is \"tom\" and I'm cool" : ok

    Putting a \backslash before a quote is called escaping the quote so that this quote does not behaves as a trailing closing quote.
    You have to escape the quote inside what you're echoing :
    echo "<img src=\"bla\">" : ok
    echo "<img src="bla">" : not ok.

  37. 137
    fillupmybox Malaysia »
    said, on 01/Dec/05 at 9:23 pm # :

    It's working now; thanks for the help. :)

    I just want to point out that there is no backslash in the 4th example, so I thought it was not needed. My bad.

  38. 138
    Word Press Plugins for ProBloggers: Blog... United States »
    pingback on 02/Dec/05 at 5:25 am # :

    […] Better Feed Click Counter IP 2 Nation Random Words […]

  39. 139
    adrian United Kingdom »
    replied, on 25/Dec/05 at 10:41 pm # :

    Hi
    Great looking plugin, but i am not getting any flag images
    Code used here

    Secton is marked

  40. 140
    adrian United Kingdom »
    thought, on 25/Dec/05 at 10:44 pm # :

    Oh and i added the (0) in the country just to test, but have also left it as () too, and still no joy, Added the to tables to SQL and populated them with the ip nation data file, the image that it tries to point to does NOT include the country code, ie its pointing to /wordpress/flags/flag_.gif

  41. 141
    Love ở Việtnam United States »
    trackback on 20/Jan/06 at 9:35 pm # :

    顯示訪客國別外掛 — IP to Nation

    這個外掛是利用ip2nation.com的資料庫寫出的, 用以顯示訪問者國別名或國家代碼, 也可以進一步利用各國旗幟來直接代表.
    這對於網站常有不明人士的拜訪留言者 , 可以利用此外掛, 顯示出…

  42. 142
    edhish Sweden »
    thought, on 23/Jan/06 at 12:23 pm # :

    Hi..

    just installed the plugin, but the flag won't show up :(
    everything is right.. dunno what is the problem.. help please

  43. 143
    Ozh France »
    said, on 23/Jan/06 at 3:13 pm # :

    edhish » obviously, everything is not right, since it doesn't work. And how am I supposed to help if I have no idea of how and where you did what ?

  44. 144
    edhish Sweden »
    said, on 23/Jan/06 at 3:30 pm # :

    ups sorry.. there is the right URL. I already upload everything according the instructions.. do you need the code that i put ??

  45. 145
    edhish Sweden »
    commented, on 23/Jan/06 at 3:40 pm # :

    rafb…

  46. 146
    Leo Blog » WordPress插件... China »
    pingback on 27/Jan/06 at 7:07 am # :

    […] IP to Nation(来访者的IP转为所在实际区域) […]

  47. 147
    IcoBgr Bulgaria »
    thought, on 27/Jan/06 at 2:16 pm # :

    Hi,
    I found this plugin and I'm tring to install it on my blog.
    But when I start the plugin I get blank page when I try to open the blog. I use WordPress 2.0.

    best regards

  48. 148
    Laptops India »
    thought, on 27/Jan/06 at 11:00 pm # :

    Thanks for it …was looking for a similar plugin ! :)

  49. 149
    Alan Canada »
    thought, on 06/Feb/06 at 6:36 am # :

    Great idea, this will be added to Hacked Gadgets soon!

  50. 150
    TEST » IP to Nation United States »
    pingback on 09/Feb/06 at 10:11 am # :

    […] http://planetozh.com/blog//archives/2004/08/27/ip-to-nation-plugin […]

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 ?