GeoTarget Wordpress plugin

Before downloading this plugin you MUST first read and follow this post or it will NOT work.

OK, this is my first ever Wordpress plugin.  It came out of a need to be able to show country specific content on a Wordpress based blog. You can use it for anything from showing a simple Bonjour to French visitors to displaying geo targeted advertising. Once it’s installed it’s pretty simple to use.

Installation instructions

1. Create the geo targeting database by following the GeoTarget database setup post mentioned above.
2. Download and unzip the plugin into your plugins folder (download link below).
3. Open the GeoTarget.php file and edit the following:-

/* USER DEFINED VARIABLES HERE */
$DatabaseName = "databasename"; // your database name
$Username = "username"; // your database username
$Password = "password"; // your database password
$DatabaseServer = "localhost"; // 99% chance you can leave this set to localhost
$CookieDomain = ".yourdomain.com"; // .yourdomain.com
$CookieDuration = 3600; // default to 3600 seconds (1 hour)

4. Activate the plugin.

How to use the GeoTarget plugin

Once installed and activated you will be able to use the $country_code variable within your theme files.  It will be a 2 character string representing the country code of the user based on their IP.  It can therefore be “US”, “UK”, “FR”, etc.

Here is an example piece of code to use within a theme file to get you started:-

<?php if ( $country_code == "FR" ) : ?><p>Bonjour</p><?php else: ?><p>Hello</p><?php endif; ?>

Download version 0.1 here: GeoTarget (downloaded 371 times)

Any questions or suggestions for improvements are welcome.

Changelog

Version 0.1

16-October-2008

  • Let’s go!

GeoTarget database setup

If you want to use the GeoTarget Wordpress plugin or you simply want to have a database you can use to determine a user’s country from their IP, this is the post for you.  This posts assumes we’re all using MySQL, PHP and phpMyAdmin.

Firstly we need to download MaxMind’s GeoLite Country database.  Make sure you download the CSV format.  Unzip it and then we’re ready for the next step.

Login to your phpMyAdmin page and select the database you want to use.  Select the ‘SQL’ tab and then paste in the following code:-

CREATE TABLE ip2country (
ip_start char(15) NOT NULL,
ip_end char(15) NOT NULL,
int_start int(10) unsigned NOT NULL,
int_end int(10) unsigned NOT NULL,
country_code char(2) NOT NULL,
country_name varchar(50) NOT NULL
);

Then hit ‘Go’.

Now select the newly created table ‘ip2country’ and select the ‘Import’ tab.  Click ‘Browse’ and select the GeoIPCountryWhois.csv file you downloaded and unzipped earlier.

Now alter the settings below as follows:-

Fields terminated by , (comma)

Lines terminated by \n

Click the image below to see how this should look.

geotarget-import-phpmyadmin-settings

Now hit ‘Go’ and wait for a while.  It’s a large file (approx 8MB) so it will take a while to upload to your server.

Once this has been completed succesfully you are now ready to use the GeoTarget Wordpress plugin.

However if you do not want to use this database as part of the Wordpress plugin or you would like to see some example PHP code to use with this database then see below.

<?php

if(isset($_COOKIE["country_code"]))
{
$country_code = $_COOKIE['country_code']; // use cookie value if set
}
else
{
$DatabaseName = "databasename"; // your database name
$Username = "username"; // your database username
$Password = "password"; // your database password
$DatabaseServer = "localhost"; // 99% chance you can leave this set to localhost
$CookieDomain = ".yourdomain.com"; // insert .yourdomain.com here
$CookieDuration = 3600; // default to 3600 seconds (1 hour)

$link = mysql_connect($DatabaseServer, $Username, $Password) or die(’Could not connect: ‘ . mysql_error()); // connect to database server
mysql_select_db($DatabaseName) or die(’Could not select database’); // select database
$ip = $_SERVER["REMOTE_ADDR"]; // get the IP address
$result = mysql_query("SELECT country_code FROM ip2country WHERE INET_ATON(’$ip’) BETWEEN int_start AND int_end"); // look up IP address

$codes = mysql_fetch_array($result); // get result
$country_code = $codes['country_code']; // two-letter country code

mysql_close($link); // clean up

setcookie("country_code", $country_code, time()+$CookieDuration, "/", $CookieDomain, 0); // sets a country_code cookie
}

if ($country_code == "GB")
{
// do something UK specific
}
elseif ($country_code == "FR")
{
// do something French specific
}
else
{
// do something non-geo specific
}

?>

That should be fairly self explanatory.  If not please ask questions below.

I know the database format and querying is not very efficient at all, and there are two excellent posts (here and here) which I intend to one day impliment myself, but for now the above method works fine for low to medium traffic sites.

What does spoffle mean?

Good question, and one I’ve heard a few times now. I’d better explain.

I first heard the word spoffle described by Stephen Fry as a word Hugh Laurie came up with when they were recording something in a studio and he wanted one of those spongy foam microphone covers. When he asked for it the word he came up with was spoffle, “umm could you get me the spoffle thing for this mic?” What is probably more surprising is that the sound technician knew precisely what he meant. In Stephen’s mind Hugh had invented a new word. This also stuck in my mind for some bizarre reason, perhaps because I’ve always been a fan of theirs ever since A bit of Fry and Laurie.

Searching Urban Dictionary it seems they agree with my memories here.

Hugh Laurie:-

Can we put a spoffle on the mike because I’m getting too much pop on the Ps.

So a spoffle is one of these:-

spoffle cover for a microphone

The spongy foam microphone cover that reduces the “popping” effect of the plosive consonant sounds as recorded by a microphone.

Another meaning?

I’ve since discovered that there is a Disney television programme called Johnny and the Sprites which started late 2005 and uses the word spoffle to describe the Sprite plant food that Ginger spreads over the plants to make them grow. See here a youtube video when things go wrong:-

No Spoffle

So there we have it, I think that’s all our spoffle word references covered.

I was hoping spoffle.com would become a place where I could write useful information for myself as well as others to resource. The word spoffle seemed appropriate (the original meaning). Rather than just regurgitating old news or reblogging everything under the sun it would be a place of originality and ‘no more noise’. Well we’ll see how it pans out, because the judges are still out on that one I guess.

Remove WordPress generator meta tag from header

Having upgraded to Wordpress 2.5.1 I discovered that if you include wp_head(); in your theme’s header.php, it now displays the following within the generated html header tags by default.

<meta name="generator" content="WordPress 2.5.1" />

Perhaps you don’t want to announce to the world exactly which version of Wordpress you are using - especially if you are slow to upgrade when new exploits become known.

The answer is to add:-

remove_action('wp_head', 'wp_generator');

to your theme’s functions.php file.

If you don’t have the file functions.php within your theme’s folder, simply make one.

Firefox about:config hacks

The world and his wife have just installed/updated to Firefox 3. I love this browser and have been using it for a long time. However, I’ve made a few hacks I thought I’d share.

Open a tab and type into the url bar about:config and then click on the “I’ll be careful, I promise!” button and you’re ready to roll. Each of the following hacks requires you to start typing in the preference name into the filter bar until you’ve narrowed it down enough to see it. Then you simply double click on it to alter its value.

These are hacks and some have potentially grave consequences, so use at your own risk.

Allowing Firefox to resize any popup windows

It’s sometimes annoying when you get a popup window that you want to resize but the web developer has set the javascript property resizable=no. Do the following to take back control.

dom.disable_window_open_feature.resizable
default set to false, double click and set to true

Disabling right click disabled

Sometimes web developers use javascript to disable your right click. Not anymore!

dom.event.contextmenu.enabled
default true, double click and set to false

URL bar declutter rich suggestions

With Firefox 3 comes a rather (in my opinion) cluttered suggestion drop down while typing in a URL into the URL bar. I’ve turned my down so I only see the top 3 results excluding any from my bookmarks.

browser.urlbar.maxRichResults
default 12, I’ve set mine to 3

browser.urlbar.matchOnlyTyped
default false, set to true to exclude bookmarks

Enable ANY and ALL Add-ons

WARNING: This hack is probably the one you shouldn’t follow if you aren’t sure about which Add-ons you’ve installed.

By default Firefox checks for compatibility and update security of any extensions (Add-ons) you have installed. This can mean when you update Firefox that some Add-ons become disabled because they are deemed not compatible. However, in reality they may still work fine.

extensions.checkCompatibility
default true, toggle to false

extensions.checkUpdateSecurity
default true, toggle to false

Gulp, I can’t believe I’ve just published that hack! Don’t shoot the messenger :)

Restoring about:config to default values

If you make a mistake you might want to restore about:config to default values. Just below the ‘Show All’ button to the right hand top corner of the about:config screen there is a little icon, click on this and choose ‘Restore Defaults’.

EDIT: Just after posting I found this great article running over 6 pages with loads of about:config hacks.