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, or display one ‘Buy Now’ button for a particular country and another for other countries. Maybe you want to display a video only for users in the US or Australia.

Developers and designers could use the plugin to load different stylesheets based on the users country. Services like hulu.com uses techniques such as this to constrain viewership to a particular country.


Supported WordPress versions

WordPress 2.5 and above. Tested upto version 4.0

Purchase Plugin

Buy Now

The download link will be emailed to you immediately on payment. Also, any future updates will be free for all buyers and will be emailed to you as and when available.

Installing the Country Filter plugin

Installation instructions are provided in the download.

>> Usage

Country Filter plugin works mainly in two ways – via a short code, and by directly adding some PHP code.

Short codes

The easiest method is to use the short-codes. This will allow you to show/hide content in posts by using the ‘country-filter‘ shortcode. For example if you want to show a certain part of your posts/page only to users who are browsing from ‘United States’ or from ‘India’ than you can use the following code. The code used are the ISO 3166-1 alpha-2 2 digit codes.

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

Setting the above shortcode in a post/page will display the string ‘Hello World!’ only for the users in US and India. People in other countries will not be able to see the string. On the other hand, setting the display attribute to ‘0’ will hide the content for users in US and India but will display the string to people from other countries as given in the following code. Note that the content between shortcodes can be anything you like.

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

Example:
Suppose you want to display a ‘Buy Now’ button in a post, maybe you are selling a product and want to add different prices – one for the users in the US and another for all other countries. You can then use the short codes as below. So now, if a user is browsing from the US the first shortcode rule applies and the text ‘Buy Now for only $45′ is displayed, on the other hand, for users from other countries the second rule applies and the text ‘Buy Now for only $32′ is displayed. As said earlier the second shortcode hides the text for the users form the US but displays for all others.

[country-filter code="us"  display="1"] 
Buy Now for only $45
[/country-filter]
 
[country-filter code="us"  display="0"] 
Buy Now for only $32
[/country-filter]

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

code : A comma separated list of 2 digit ISO 3166-1 alpha-2 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.

Nesting Short codes

You can nest your other plugin short codes in between the ‘country-filter’ short code to control the visibility of the other plugin data. For example, the following code nests the WordPress ‘gallery’ shortcode, which will now only be displayed if the user is browsing from the US or India.

[country-filter code="us, in"  display="1"] 
[gallery]
[/country-filter]

Maybe you have a form plugin which provides a ‘[form]’ shortcode and you want to display the form only for users NOT from the US. The following gives an example for that particular scenario.

[country-filter code="us"  display="0"] 
[form]
[/country-filter]

Another use case frequently needed by users – to show / hide PayPal buttons for different countries. For example the following will show the PayPal ‘Buy Now’ button for US users only.:

[country-filter code="us" display="1"]
 
<form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="dummy-email@yahoo.com">
<input type="hidden" name="lc" value="US">
<input type="hidden" name="item_name" value="Design e-book">
<input type="hidden" name="amount" value="12.00">
<input type="hidden" name="currency_code" value="USD">
<input type="hidden" name="button_subtype" value="services">
<input type="hidden" name="no_note" value="0">
<input type="hidden" name="bn" value="PP-BuyNowBF:btn_buynowCC_LG.gif:NonHostedGuest">
<input type="image" src="https://www.paypalobjects.com/en_US/i/btn/btn_buynowCC_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="https://www.paypalobjects.com/en_US/i/scr/pixel.gif" width="1" height="1">
</form>
 
[/country-filter]

Filtering other content

You can also filter content other than posts with the isCountryInFilter function. Note that for this you will need to make changes to sections of the theme files. For example if you would like to display some content – like a button, a widget or some section of a web page, that should only be 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 } } ?>
.
.

You can also add a else section.

.
.
<?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 } else { ?>
 
// People from other countries will see the content
// given here but not the above.
 
<?php } } ?>
.
.

This is a powerful way to control content on your WordPress site. You can control almost any type of content with this method. You can hide/display entire sections of WordPress depending on the users country.

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. Note that this function is especially useful for developers, as they can include custom code and other files depending on the users country.

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 } } ?>

Take another example – we want to display different banners depending on if the user is from the US, the Netherlands and a third banner for all other users.

<?php if (function_exists('showCountryContentInPage')) { ?>
<?php if(showCountryContentInPage(array("us"),1)) { ?> 
 
// The banner here is displayed only for users browsing from US.
 
<?php } elseif(showCountryContentInPage(array("nl"),1))  { ?>
 
// The banner here is displayed only for users browsing from Netherlands.
 
<?php } else  { ?>
 
// People from other countries will see the content
// given here but not the US, or Netherlands (nl).
 
<?php } } ?>

Another example – you want to display a certain banner in the sidebar (or anywhere else) which will be visible to all the users from all countries except the ‘US'; in short, we want to hide the banner for users from the ‘US’ only.

<?php if (function_exists('showCountryContentInPage')) { ?>
<?php if(showCountryContentInPage(array("us"),0)) { ?> 
   <img src="images/some_banner.jpg" />
<?php } } ?>

Dashboard

The plugin dashboard also displays the total number of times any content has been filtered by geographic areas. Note that data logging is disabled by default so you will need to enable it from the plugins admin section to be able to see the data on the map.

You can also export the generated data as a CSV file, which you can further analyze in a spreadsheet.

Troubleshooting

Plugin fails to activate
If the plugin fails to activate and displays and error, check the table names in the database, they are case sensitive. They should be ‘ip2nation’ and ‘ip2nationCountries’ if you are using the ip2nation Version of the plugin. This will work on Windows but will fail on Linux systems as the files there are case sensitive.

Plugin gives inaccurate results
This can happen if your PC is behind a firewall, proxy or a VPN. This programs tend to hide your original IP address, which can result in incorrect results. This is something the plugin, or for that matter any other plugin, cannot do anything about.

FAQ’s

Will the plugin work with mobile devices and sites.
As long as your mobile device provides a correct ip address the plugin will work without any problem. Go to a site such as http://www.whatismyip.org/ or http://www.whatismyip.com/ to check the ip address of your mobile.

Some of the sites using the plugin

Purchase Plugin

Buy Now

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.
# Version 2.0
* ADDED: Users visualization by country.

157 thoughts on “Country Filter WordPress plugin

  1. Hi, is there a option for a City Filtrer?
    I only work in spain, and I nedd show diferents image depending of the diferent city.
    I hope your answer.

  2. Now I have seen post N 145 of RAJESH.
    You told him “Currently it cannot be used to filter by city.”
    But Do you are working in that?
    When users can have this option?
    1 month?
    6 month?
    1 year?
    Thank’s

  3. Not within 1 month for sure, but will be available in the next 6 months. Actually the problem lies with the db size for cities,which is kind of huge compared to the countries database.

  4. Hi sameer.
    Thank’s for your answer.
    I’m not informatic but I understand what you mean.
    Only two suggestion in order to make a good plugin:
    1. The City option must include a radius where administror can introduce radius value (ex 50 km.). So, picture will showed also at the users next to the “CITY”.
    2. The administrator interested in “CITY OPION” in the 90% of case only works in a specific state. For ex. I am working in spain, and I need “CITY OPION” only for spain. Maybe also for argentina. But I don’t need “CITY OPION” everywhere.
    So maybe you can solve the size of data base, offering especific “Cities of Country addons”. So for exemple I only installe “Cities of Spain addon”. I don’t need the other cities…
    Do you understand what I sayd?
    I hope your news about that.
    Good work.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>