planetOzh, IP to Nation WordPress Plugin, Published

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)

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 :

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 :

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.

I like the idea of this plugin, but I am having a hard time getting it to work with WP v2.9.2. PHPMyAdmin is installed on my server by my web host and I use it to manage databases for several domains. I have tried both importing the sql data to my wp database and creating a completely new one populated by the imported sql. I then installed your plugin via wordpess admin and activated it. I could not find any configuration option for the plugin, so next I just added the following code based on your example to my theme's sidebar to see what would happen:

All I end up with is "You are probably from" and "If so, your country flag is" without the country or flag displayed. I figure the problem is most likely database related, but I do not know how to fix it and without some kind of config option don't know where to start.

Jared, probably you didn't imported the sql correctly, and tables are not being found. This plugin doesn't have admin page as I remember, you just need to have the ip2nation tables and use its functions.

Take a look on my plugin Hikari Enhanced Comments, it's based on this one and allows you to have ip2nation tables in a unique database and share it among all WP sites. Of course you must config database user access and so on, but it may help you.

Is this because i use wp 2.9.2 or should we install those two tables in a specific database.

I looked through the plugin and can't figure out where you call the database, thus I'm assuming you're working on the current database, which should be the one for the WP. Still, I can't really make it work.

Ozh what do you suggest as a fix when changing the file upload size is restricted (changing it it's not an option on this server) but the IPNATION sql file is bigger than the server allows? Can it be run from somewhere else. I really want to use your option, it's such a time saver for me. Any help would be tremendously appreciated.

I am trying to get the flags to show. When I inserted the code I received an error so I removed it. In reading your instructions and I see I am to add the script when I see , etc. I am using 2011 theme and do not see this anywhere in the blogroot/wp-content/themes/yourtheme/comments.php. Could you tell me where the code goes in this instance.

Read more ?

I love WordPress plugins so I wrote a book!

This site

This is planetOzh, weblog of Ozh, a 35 years old dude living in France with an interest for, errr, like, computer things. I have a wife, 2 kids, and one computer-unrelated job. Give my eye a gentle click to read more about me.