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
nder: Computers — site admin @ 12:16 am
Just added a plugin, created by Planet Ozh, that guesses the origin of a comment by its IP address. So […]
I have installed this plugin and everything is OK, with one exception. The flag gifs that display are only for the Philippines, the country where I'm located. The comments from other countries show the same PI flag.
The site is the, Cranial Cavity weblog.
Any ideas to cure this problem.
where & what code did you use ?
I have installed the plugin, created the tables but I'm not sure where to put the code.
well … it depends what you want to do with the plugin … The code needs to be in a .php file within php tags (<?php and ?>). Just copy & paste the examples I give, or be more specific with your question if you can't find the answer here about what you want to do.
I'd like to display flags after comments. I guess I can copy and paste your first example (?) but where? wp-comments.php?
You should probably read the last paragraph of this article.
I've read the whole article and all of the messages. It would help beginners if you could spell it out a little more clearly.
to print your commenters' country : in wp-comments.php, where you see <?php comment_author_link() ?>, put the following :
<?php comment_author_link() ?> from
<?php wp_ozh_getCountryName(1,$comment->comment_author_IP) ?>
Doesn't this answer your question ?
I guess the plugin isn't compatable with WordPress 1.5. There is no file in 1.5 called wp-comments.php.
The plugin is compatible, you just have to guess what changed with 1.5 ..
A quick look in /wp-content/themes/(your theme or default)/comments.php is enough to guess you have to modify things here.
very cool script!!! ill use it!
very cool script you have hear. i eventually got an overseas person to test my script out but it only shows one country. ie my country as their flag. I followed everything pretty closely.. Is there something i missed.
I'd really like to get this working. Thanks Mate!!
Please be more accurate, I can't help with so few info. What code did you use, and where did you put it ?
I didnt put the comment_author_link edit you gave. I thought that was optional
In the end i decided to put that alongside the code where you said to place it and the rest of the code
which was the wp_ozh_getCountryName which i modified a little bit in the comments section.
Anyways without the comment_author link edit, it would show all users as australian. Now that its there, it works. Do i really need this to get it work?
From how i read it i thought it would show the ip. I rather that not be the case.
It works tho and i thank you for your time and im sorry for my misintepretation of what you wrote there.
I just assumed it wasn't necessary.
If i double post my mistake it wouldnt let me put in the code that i used.
hi its me again. after posting that comment_author_link
it now says like bryan is from australia says:
instead bryan says:
i rather have it say bryan says:
though it shows the flags correct for the overseas person, my first comment i made, ended up saying i was from finland but the other test post came out correct.
wierd. any suggestions?
Well, can't say much more than I already explained in this page, dude. You can pass 2 parameters to wp_ozh_getCountryName, the first one setting if you want to echo the result or not, the second one being an IP, defaulting to current visitor's IP if nothing stated …
If at least you provided an URL where to look at …
it's cool i just modified it a little bit to make it work the way i wanted it. thanks.
here is what i did. just moved a few things around til i got what i wanted aesthetically
I ended up moving the echo away from this code so that it wouldn't repeat the country again.
wp_ozh_getCountryName(1,$comment->comment_author_IP)
it seems very good n now i try to test to see a norwegian flag come up or not, :-)
It works. It clearly shows that you're in the Austrian part of Norway.
excelent, thank you
Hi there!!
This is amazing!! really cool! Althou phpmysql is better it deppends on the server because not all of them can manage 18000 queries hehe. Anyway, i wanted to show not the name but the flags in the comments so I managed to get this:
comment_author_IP) . ".gif">"; ?>
But then i get for example for Peru: pe[Peruvian flag] How do I get rid of the prefix? for Spain i get the "es" and so on…
Thanks in advance :)
Don't understand what you're asking for. I think that examples provided here explain how to display ieither a flag or a comment name, just follow the guidelines detailed here.
I have this in my code:
<? echo "<img alt="your flag" src="/images/flag_" .
wp_ozh_getCountryCode(1,$comment->comment_author_IP) . ".gif">";
?>
But then, the comentor get this http://weblog.homelinux.org/archivos/2005/02/17/65/#comments
See the country code before the flag.
Saludos
Alejandro
This is exactly the code I have here. The code you pasted outputs only an image tag, so you obviously have something *before* it. Paste your complete code somewhere I can read it (not here)
hehe, as you see, i have wp_ozh_getCountryCode(1,$comment->comment_author_IP) but playing a little bit, i ended changing that 1 to 0 and the country code :) sorry to bother
merci :P
Alejandro
PS: good idea isn't it? i see you've also implemented it :P
"(…) and the country code was gone (…)"
I had it implemented since the day I wrote this plugin. It just disappeared when I upgraded WordPress and overwrote the comments.php file :)
Oh! I didn't knew that :) but it is a cool thing. I might be changing it to "writing from" because I'm from Peru, but living in Finland….so sometimes the flag is not where we come from :P well it's just a thought :)
Saludos
Alejandro
I am sorry but I cant get the flag and the name to appear. I want my comments form to say Comment by Jewel from (flag) United Kingdom, but I cant get the flag to appear at all. I have put the flags in "http://wwww.jewelswebgraphics.com/news/wp-images/flags" and have used an absolute url to call it but to no avail. When I copy and paste your example code, I keep getting parse errors.
Can you help at all please? (php newbie)
thank you
[…] In an effort to add more flair to this site, I have incorporated Ozh's IP to Nation Plugin which kindly tells me which country som […]
Jewel » obviously with no info on your "parse errors", I can't do much. Paste somewhere (*not here*) the code you're using and mail me back when it's done
I'm soor for the last comment, the error is: img alt="
Fatal error: Call to undefined function: wp_ozh_getcountryname() in /usr/home/sakrajda/domains/sakrajda.info/public_html/log/wp-content/themes/default/comments.php on line 30
It seems you didnt activate the plugin.
I'm gonna test it with 1.5, and try to find some more cool stuff to do with it :)
Very good idea to put that in a plugin btw. Great thinking (AND great explanation for non-coders!).
It works great with 1.5, although here are some remarks:
1.5 uses themes, so the file you want to edit is "/wp-content/themes/default/comments.php" or in whatever directory your theme is.
An example of the code you could use to display a flag before the commenters name:
comment_author_IP). '.gif'; ?>" width="18″ height="12″ alt="comment_author_IP); ?>" /> says:
Don't forget to change the path's to your flag-images.
Great plugin, thanks a lot!
[…] kmark" title="Permanent Link: "> I was searching for cool plugins and found this one, it looks up the country an IP originates from (by using ip2na […]
[…] WordPress IP to Country Plugin Wordpress IP to Country plugin is adapted from IP-to-Nation plugin for WordPress. The only difference is that my IP to […]
cool stuff
I think you should specify in the instructions to add the SQL file to your WordPress database — and that you don't need to create a new one like I did :-P
Anyway, great plugin!
[…] « Bad Taylor. | IP to Nation. Well, I finally got the IP to Nation plugin working after some instruction problems and IP to C […]
[…] lag next to their name. Check out this article from the guy who packaged it for WordPress I followed his instructions completely and they worked for me, a coup […]
[…] erInfo v1.8 2. ip2nation (Per Gustafsson Consulting) for country codes and names tables 3. Ozh for his IP to Nation plugin Download j […]
Is it possible to fix bad recognizing IP? Both IP 202.81.205.22 and 221.134.1.43 are localized in India, but the second one has got Vietnamese flag. ;)
[…] 自行æ›ä¸Šè‡ºç£åœ‹æ——,真å—ä¸äº†ä¸åœ‹åˆ°è™•éƒ½è¦æ‰“å£“çš„æƒ¡è¡Œï¼ ç›¸é—œç¶²å€ï¼šIP to Nation WordPress Plugin æ¤ç¯‡æ–‡ç« 發è […]
cool, i will promote it among my friends
this plug in it's cool but don't work with many countryes, for example this…nicaragua or some ips of costa rica
[…] Sepertinya gue harus nyari alternatif laen,.. Namanya IP TO NATION. Katanya seh sama aja. Yang ngebedain itu cuma databasenya […]
Again, please DO NOT post junk comments just to test this fu**ing plugin. Have a fu**ing look at the example page mentionned at the beginning of the page. God I'm tired of deleting comments everyday.
I've uploaded the sql file using phpmysql, and the sqlquery confirms the 18,000 instrustions, but no table was created. Can I create one manually? How many fields? etc. I'm unable to upload the zip file, had to unzip then upload. Sorry for this question, but can't use the plugin without the table.
Thannks for any help
Garnet