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.
Related posts
Shorter URL
Want to share or tweet this post? Please use this short URL: http://ozh.in/1t
Metastuff
354 Blablas
Pages: [36] 35 34 33 32 31 30 29 28 27 26 … 1 » Show All
Pages: [36] 35 34 33 32 31 30 29 28 27 26 … 1 » Show All
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.
replied, on 05/Feb/10 at 1:16 am # :
I love this plugin!
commented, on 25/Jan/10 at 10:06 pm # :
Hikari » fixed, get it here
replied, 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)