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
Hi Ozh,
I have a problem with this great plugin and I hope you can help me.
I used this code withing the comments.php:
<img alt="comment_author_IP) ?>" src="http://www.donguriyama.net/blog/px/flags/flag_comment_author_IP) ?>.gif">
The problem is, that every comment I (or anyone else) write gets a German flag, although I'm located in Japan. My webspace host is in Germany though.
Do I have to modify something with the code I used, or does my host has a problem with this?
Argh, sorry again.
Please just look at it here: http://blog.donguriyama.net/code.txt
sven » looks like $comment (and therefore $comment->comment_author_IP) is empty (what happens if you just "echo $comment->comment_author_IP" ?). Are the commenters IP correctly recorded in your database? (table wp_comment, there should be a column containing IPs). Did you enclose the flag displaying in a custom function and you forgot to issue a "global $comment" inside it?
Cannot be more helpful than this, sorry…
Hi Ozh!
Thanks for your reply!
I checked the wp_comment table and I figured that your IP is the same with mine (as well as with some spam I got earlier). That means, for some strange reason, the IP is just recorded wrong… It's always the same…
I guess it can only have something to do with my host then, right?
sven » could be a server side problem, indeed. There must be some kind of local proxy filtering connections, or something like this. This is very problematic, as it can interfere with a lot of other things (spam detection for example). You should ask your server admins to check this.
Ya, I thought something like that, too. But I already talked to my webhost's support team and they uploaded a small script to my webspace: http://donguriyama.net/testip.php
As you can see, it shows the correct IP there. That means no server problem… The problem must be somewhere within wordpress itself or the plugin. :(
Do you think it could have something to do with my current theme? I remember that ip2nation worked correct on my blog a few months ago, but that was under an another theme… Although, no it seems to not work with any theme I try… :(
Kind of have no clue here…
sven » the plugin doesn't store any IP. It just reads the IP that has been stored previously. The problem must be coming from another plugin, if it used to work. I don't think it can be theme related. The question you have to ask yourself is: what plugin can interfere with WP at the time of *posting* a comment?
Even I tried to deactivate all plugins (except ip2nation of course), it still has the same error…
May there be anything else than plugins that could possibly interfere with posting comments?
sven » sent you an email
is it possible to show the cities names?
Riri Audiya » No.
very cool plugin doest work with latest version of wordpress?
Ozh, you are great =D
I think I can use all your plugins, because it is soo damn good!
I am going to put this flag things in my blog, thank you!!
=D
I have this error, could tell why,
Parse error: syntax error, unexpected ';', expecting T_STRING or T_VARIABLE or '{' or '$' in /home/cdgen0r/public_html/pruebadepaternidad/wp-admin/moderation.php on line 167
Thank you very much
walter » probably not related to my plugin.
is it work really?
can you make this only visible in wordpress admin comment manager?
mory » Interesting suggestion. I'll add this feature to a next release.
hello,
I've imported the SQL file, and uploaded the folder to my plugin folder, but the plugin does not appear in my plugin list in admin panel!!
I'm using WP 2.5
what's the reason?
Yeap…having the same problem here. WordPress 2.5 doesn't show up in the plugin list.
Faiz & Seyyed » OOPS! My bad! Re download the plugin, or edit it and on the very first line replace "Hack Name" with "Script Name". Really I don't know how such a typo got there :)
Hi Ozh, its working now. Thanks! :-)
As for Seyyed, have you checked the path for the flags? the default path is "/images/flags"
Is it same with Priyadi's Plugin?
And I see in Priyadi's blog, there're show commenter's cities name, do you know how to activate the cities name?
Anggie » Priyadi's plugin was made after this one with another geo database which is a lot bigger (and which, apparently, tries to resolve IP to country *and* city, although I guess it's probably approximative). No city with my plugin.
hi. great and easier plugin plus a very accessible maker of the plugin. could you modify this with city included? it will blast priyadi's,lol..
Resolved: I just deleted the cache file for ip to nation…
I had installed the useronline user first, then your ip2nation, and only then did I RTFM ("read the fucking manual"…) and installed the sql.
It works marvels, and I love it.
[…] ????????? ? ?????, ?????????? ??? ?????????? ??????? ??????? ip2nation ??? ???? ??, ?? ?? ???????? ????? ??????? […]
Hi,
I got some problem with this plugin. It worked very nice until yesterday. Since then I get some fwrite() and fclose() error for every comment. Look here
How can I solve this problem?
Sven Schellberg » if it worked fined till yesterday, ask yourself what you've changed then. Looks like the wp-content/cache directory is missing maybe.
ok, its done… I have to delete the ip2nation folder in wp-content/cache but I don't know why
Now it works again
I'm CSS challenged. I've activated the plugin, but can't seem to make it work. Do I have to add some code to my template?
Shoshana » Please read the doc here.
Ozh
I'm getting the following errors when I try to activate the plugin
Warning: mkdir() [function.mkdir]: Permission denied in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 27
and
Warning: mkdir() [function.mkdir]: No such file or directory in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 28
any ideas?
Thanks
Greg
Greg » make sure there's a wp-content/cache/ directory. I will improve some checks when I upgrade this plugin
Ozh
Ok I created the cache directory, chmoded it to 755 and that took care of one message.
Now I\'m getting
Warning: mkdir() [function.mkdir]: Permission denied in mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php on line 28
and when I go into the wordpress admin interface to deactivate it, I get
Warning: Cannot modify header information – headers already sent by (output started at mysite/wp-content/plugins/ozhs-ip-to-nation/wp_ozh_ip2nation.php:28) in mysite/wp-includes/pluggable.php on line 694
sorry to be such a pain ; )
Greg
Okay, I noticed that this plugin caused the website lagged and slow when activated on version 1.2.1.1 especially try to loggin into wp-admin is the worst part when it comes to lagging. I think that it had to do with the cache or something like that. Not sure. Any idea how to fix it?
LeviathanC » I don't notice any particular lag on your website. Plus, the plugin is totally inactive in the admin area, so it's not doing anything when you log in.
Ozh,
I figure out that one of the plugins lagged my site down and had to disabled it for awhile. And my apology for being mistake on that.
Hi,
I am getting the exact same messages as Greg. I am using WP 2.5.1.
Any ideas as to what the problem may be?
Since the error suggested that it wasn't being allowed to make directories, I looked in the php file to see what other directories it was trying to make.
As already mentioned by Ozh, you need a wp-content/cache directory. You also need a wp-content/cache/ip2nation directory. That took care of the error messages that Greg mentioned that I was also getting so I assume that it is working now.
Ann & Greg » I've modified the plugin, please download it again if you still get errors.
Ozh, I downloaded it again. I am getting the following error when I try to use either your example for determining whether someone speaks French and when I use the Who-Sees-Ads plugin using your US readers only example. I got this error with the old version too:
Any ideas? I'd love to get this working as I'm trying to get the geotargeting of ads working.
hi,
just to let you know, this plugin has crashed my blog in 2 webhosts! first, my local webhost kicked me out because of server load caused i'm pretty sure by this plugin because it is the only plugin i installed before the crash and when i removed this everything went back to normal..it caused a heavy database load when traffic increases..but i really loved this plugin and what it can do, so when i transferred to a world famous web host i thought it can handle the load, wrong assumption, this dream host also crashed and informed me that they will kick me out if the heavy server load will not be solved, and the only plugin i installed is this one. they disabled all my plugins except the anti-spam ones. and it solved the database crash..i activated all plugins one by one thereafter, except this ip to nation and everything are back to normal.
i really want to use this amazing feature, esp. since i have a large international traffic, i hope you update or fix this heavy database/server load use of this plugin..
Get It From Boy! » There's nothing to fix except your host.
Each IP that's stored with a comment needs an SQL query the first time, and then it's cached on disk so there's no additional query. I'm using it here, several posts have 300+ comments, and I never had any problem with it.
hi ozh,
that's strange, we have the SAME host..i noticed you have dreamhost invites..so i figured, you're hosted there too..
Get It From Boy! » Yes, I am hosted on DH
There are some places that when I post from, neither the flag or the country name appears, and there are other places that works fine. Do you have a clue of what can cause this?
Thanks in advance
Sorry but your database is wrong !!
I'm from Viet Nam but the flag is Australia
So how can I update the database ?
Ozh, any ideas on the error message I posted?
Ann » Sorry, I don't know… At least you could prefix the lines with "@" (ie "@fwrite" and "@fclose") to remove the warnings and make things silent. You sure there's a cache folder specified and that it's writeable ? (see Codex)