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
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 :
- <?php
- echo "You are probably from ". wp_ozh_getCountryName() ."<br />";
- echo "If so, your country flag is <img alt=\"your flag\"
- src=\"/images/flags/flag_"
- . wp_ozh_getCountryCode() . ".gif\"><br />";
- ?>
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 :
- <?php
- $country = wp_ozh_getCountryName(0) ;
- switch ($country) {
- case "France" :
- case "Guadeloupe" :
- case "Luxembourg" :
- case "Monaco" :
- case "Martinique" :
- case "New Caledonia" :
- case "French Polynesia" :
- case "St. Pierre and Miquelon" :
- case "Reunion" :
- case "French Southern Territories" :
- case "Wallis and Futuna Islands" :
- echo "you speak French";
- break;
- default :
- echo "you may not speak French";
- }
- ?>
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 :
- <?php comment_author_link() ?> from
- <?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 :
- <?php
- echo '<img alt="your flag" src="/images/flags/flag_'
- . wp_ozh_getCountryCode(0,$comment->comment_author_IP)
- . '.gif">' ;
- ?>
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
[…] 7;s Browser Sniff plugin to detect visitor's browser and OS. Installing Ozh's Ip united.com">2 Nation plugin to det […]
[…] 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) […]
Love your script.
[…] ; 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 […]
[…] plicar o no los filtros del CMS antes de mostrarse. IP to Country: Versión del plugin […]
[…] 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 […]
Excellent plugin my friend.. I shall be using this when I move to my wordpress blog at http://www.arrogantly.com
This plugin is great as it adds color to the overall site. Superb!
[…] 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 […]
[…] 표시만들기
June 7th, 2005
pionelle–>
– ì°¸ê³ : IP to Nation WordPress Plugin by Ozh 설명히 ìžì„¸í•˜ê²Œ 나와있ì […]
[…] ´ì½˜ 만들기
June 7th, 2005
pionelle–>
– ì°¸ê³ : IP to Nation WordPress Plugin by Ozh ì´ Pluginì„ ë‹¬ë©´ ìƒëŒ€ë°©ì˜ […]
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.
Well, first tell me what code you tried to use …
Hi,
Here is the code
Thanks in advance.
These 2 lines look correct… So the problem is somewhere else in your page.
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.
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.
[…] ncy Tooltips Flickr Gallery flickrRSS Jerome's Keywords Kramer Subscribe to Comments IP2Nation Cat2Tag There are a few plugins beyond th […]
[…] – ì°¸ê³ : IP to Nation WordPress Plugin by Ozh ì´ Pluginì„ ë‹¬ë©´ ìƒëŒ€ë°©ì˜ ipì— ë”°ë¼ ì €ìž¥ë˜ì–´ 있는 170ì—¬ê°œì˜ ì´ë¯¸ì§€ì— ë”°ë¼ êµê°€ë³„ êµê¸°ê°€ ì•„ì´ë”” ì˜†ì— í‘œì‹œê°€ ëœë‹¤. 설명히 ìžì„¸í•˜ê²Œ 나와있지만, ì¼ë‹¨ ì˜ì–´ì´ë¯€ë¡œ 쉽게 한글로 설명해보ìž. […]
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 :(
TinyGuy » doh… too bad for you. This seems to be a misconfiguration of your host, try to get in touch with your web admin.
[…] […]
ehmm.. Ozh..
I've just moved to WPress!
Well, i aim to use your plug-in(s), thanks dude! ^_^
i am using the plugin as well. i did make some modifications for it to work better with my weblog though…
thanx
Cool idea! :D
great job dude
nice Work.
Very Cool script! It works very well!! :D
[…] WordPress显示留言者地区的æ’件IP to Nation。 […]
Hey man, great plugin, really appreciate all the work you must've put in. Thanks again.
[…]   IP to Nation,å¯ä»¥æŠŠæ¥è®¿è€…çš„IP转为所在实际区域。 […]
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
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.
when you echo "something", escape the quotes inside something …
Sorry, I don't quite get it. I'm not much a technical savvy guy. Hope you can explain it in details.
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.
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.
[…] Better Feed Click Counter IP 2 Nation Random Words […]
Hi
Great looking plugin, but i am not getting any flag images
Code used here
Secton is marked
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
顯示訪客國別外掛 — IP to Nation
這個外掛是利用ip2nation.com的資料庫寫出的, 用以顯示訪å•è€…國別å或國家代碼, 也å¯ä»¥é€²ä¸€æ¥åˆ©ç”¨å„國旗幟來直接代表.
這å°æ–¼ç¶²ç«™å¸¸æœ‰ä¸æ˜Žäººå£«çš„拜訪留言者 , å¯ä»¥åˆ©ç”¨æ¤å¤–掛, 顯示出…
Hi..
just installed the plugin, but the flag won't show up :(
everything is right.. dunno what is the problem.. help please
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 ?
ups sorry.. there is the right URL. I already upload everything according the instructions.. do you need the code that i put ??
rafb…
[…] IP to Nation(æ¥è®¿è€…çš„IP转为所在实际区域) […]
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
Thanks for it …was looking for a similar plugin ! :)
Great idea, this will be added to Hacked Gadgets soon!
[…] http://planetozh.com/blog//archives/2004/08/27/ip-to-nation-plugin […]