Country Filter Wordpress plugin


Description

Country Filter plugins allows you to control the visibility of Wordpress elements depending on the users country of origin. You can hide or show certain elements of a page or some sections of a post from users browsing from a particular country. For example you could have a ‘donate’ button that displays only if the user is browsing from France.

Supported versions

Wordpress 2.5 and above

Download Version 1.5.

>> Installation


Installing the Country Filter plugin

1. Upload the entire country-filter directory to the /wp-content/plugins/ directory.
2. Activate the plugin through the ‘Plugins’ menu in WordPress.

Installing the ip2nation database

ip2nation is a free optimized MySQL database that maps ip addresses with their respective country names.

Note: If you have any plugin that already uses the ip2nation database, like Lester Chan’s wp-useronline, than you do not need to reinstall the database. The ‘country-filter’ plugin uses the table names ‘ip2nationCountries’ and ‘ip2nation’ in the code. So if your previous ip2nation installation uses a different table name, you can change the table names in the ‘country-filter’ plugin, or if you are not comfortable with modifying code, than go ahead and install a fresh copy of ip2nation with the steps given below.

1. Download the database from http://www.ip2nation.com/ip2nation/Download
2. After the download is complete extract the ip2nation.sql file from ip2nation.zip
3. Go to your domains phpMyAdmin webpage
4. Select your WordPress Database and click on Import Tab
5. Select the ip2nation.sql SQL file and click Go
6. phpMyAdmin should take a minute to create two tables in your wordpress database : ip2nationCountries and ip2nation.

>> Usage

Short codes

You can show/hide content in posts by using the ‘country-filter‘ shortcode. For example if you want to show a certain part of your posts only to users who are browsing from ‘United States’ or from ‘India’ than you can use the following code.

[country-filter code="us, in"  display="1"] 
Hello World! 
[/country-filter]

This will display the string ‘Hello World!’ only for the users in US and India. Setting the show attribute to ‘0′ will hide the content for users in US and India.

Setting the ‘display’ attribute to ‘0′ for the above will hide the content for users in ‘India’ and the ‘US’ but not the rest.

[country-filter code="us, in"  display="0"] 
Hello World! 
[/country-filter]

The short code takes two attributes, both of which are mandatory.

code : A comma separated list of country codes.
display : Set to ‘1′ if you want to display or ‘0′ if you want to hide the content. If set to ‘0′ the content will be hidden for the given countries but displayed for others.

Filtering other content

You can also filter content other than posts with the ‘isCountryInFilter’ function. For example if you would like to display some content – like a button or some section of a web page, that is only visible for people browsing from US, UK or Australia than you can do it as follows.

.
.
.
<?php if (function_exists('isCountryInFilter')) { ?>
<?php if(isCountryInFilter(array("us", "gb", "au"))) { ?> 
 
// The content here is displayed only for users 
// browsing from US, UK or Australia.
 
<?php } } ?>
.
.
.

Note that the ‘isCountryInFilter’ function takes an array of country codes and returns true if the country code of the user browsing the site is in the array.

Another function added in version 1.5 is showCountryContentInPage. This function is just like the short code version above, but can be used to manipulate content other than posts. This takes an extra ‘display’ parameter after the countries array. If set to ‘1′ than the content will be only displayed for the given countries. If set to ‘0′ than the content will be hidden for the given countries but displayed for others.

<?php if (function_exists('showCountryContentInPage')) { ?>
<?php if(showCountryContentInPage(array("us", "gb", "au"), 1)) { ?> 
 
// The content here is displayed only for users
// browsing from US, UK or Australia.
 
<?php } } ?>

Development:

» http://dev.wp-plugins.org/browser/country-filter/

Changelog:

# Version 1.01
* FIXED: Problem between spaces in country codes
# Version 1.1
* ADDED: Support for nested shortcodes
# Version 1.5
* ADDED: New function and changed some logic.
Thanks to Matt of NuFlux Media Inc for providing excellent feedback.

RECENT COMMENTS

ON TWITTER