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!

Dustin wrote,
How would you add more then one country to the code? Commas or what?
For example:
BonjourHello
Link | October 21st, 2008 at 4:37 am
Neil wrote,
Hi Dustin
Not certain precisely what you’re asking, but I can give you a couple of examples of how to test for multiple countries at the same time.
Firstly you could test if the country_code is either FR or CA then display ‘Bonjour’, else display ‘Hello’ to everyone else, like so:-
<?php if ( ($country_code == "FR") || ($country_code == "CA") ) : ?><p>Bonjour</p><?php else: ?><p>Hello</p><?php endif; ?>Secondly you could do multiple if tests. For example, if country_code is US then display ‘caseUS’, else if country_code is UK display ‘caseUK’, else display ‘caseOTHER’ for everyone else, like so:-
<?php if ( $country_code == "US" ) : ?><p>caseUS</p><?php elseif ( $country_code == "UK" ) : ?><p>caseUK</p><?php else: ?><p>caseOTHER</p><?php endif; ?>In both examples you can have multiple OR (||) or elseif statements or even use the two together to achieve what you want using the country_code variable.
I hope this answers your question.
Link | October 21st, 2008 at 9:22 am
Huy Zing wrote,
Thanks for the work. I just had one problem.
I had to comment out this line:
// mysql_close($link); // clean up
Otherwise, I would get all these errors:
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_affected_rows(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 621
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Warning: mysql_error(): 9 is not a valid MySQL-Link resource in /home/www/wp/wp-includes/wp-db.php on line 615
Link | May 4th, 2009 at 3:26 pm
Neil wrote,
Thanks for the tip, I guess $link must be used elsewhere, and closing it was a BAD IDEA. At least others can see how to fix it if their stuff breaks in a similar way.
Link | May 7th, 2009 at 10:51 am
Invest Money Lab wrote,
Hi Neil,
I have tested the script. The PHP works with the $country_code variable in the theme file but it does not seem to work if I insert the PHP code in my POST. I use the following plugin to enable PHP in my post.
http://wordpress.org/extend/plugins/exec-php/
Thank you.
Link | May 13th, 2009 at 1:10 pm