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


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.

176 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.

  5. I dont know why this plugin doesnt work on my website. The country code doesnt, the visibility is only control by display=””, content hide if it is 1, content show if it is 0, no matter what country code put. For example my current location is in UK, and I put
    [country-filter code=”gb” display=”1″]
    Hello!
    [/country-filter]
    This doesnt show anywhere, even I am in uk. Change display to “0”, now content is showing to all countries (checked on VPN)

    Could you please help?

  6. Hi I have tinkered with an older plugin called Content by Country but it seems a bit limited and Im having issues trying to get it to work (it used to work). I am interested in purchasing this plugin but what I really need it to do is swap out menu items using the WP built in menu functions. Can this be done with this plugin and are you able to offer any support on how to get it to fork. An example would be a person in the US will see a vertical menu with 6 items (products) but a person from UK will have 7 items available (products) which will be the original 6 plus one additional unique to UK. Thanks. -Eileen

  7. You could use the ‘isCountryInFilter‘ PHP function in the plugin to do that (look at the example above). However, I’m at present not free to support any coding.

  8. Hi i want to buy plugin. But ı use quick adsense for banner ads and ı want to target ads by countries. But i want to know that it is compatible or not with quick adsense widget ?

  9. Just tested and it works. For example to display ‘Ads1′ in a particular location in the post one could use the following:

    [country-filter code=”us, in” display=”1″]
    <!–Ads1–>
    [/country-filter]

    This will display the particular ad for only people from US and India. To display to all people and ONLY hide for users in US and India:

    [country-filter code=”us, in” display=”0″]
    <!–Ads1–>
    [/country-filter]

    Note that you will need to manually add this in the post or pages where you want to control the ad display. Automatic insertion of ads by Quick Adsense will not use country filter.

  10. I have a question
    I have purchased and installed the plugin, I’ve configured to filter by these countries (us, mx, co)
    But the statistics I see that there are visitors from other countries of the world … How is this possible?
    Is the plugin works well?

  11. Hello, I bought your plugin and activated it. When I use it on my page for first time it does not work. I get the following errors:

    Notice: Undefined index: export_data in /Applications/MAMP/htdocs/corpak-local/wp-content/plugins/country-filter/country-filter.php on line 37

    Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/corpak-local/wp-content/plugins/country-filter/country-filter.php:37) in /Applications/MAMP/htdocs/corpak-local/wp-admin/post.php on line 233

    Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/corpak-local/wp-content/plugins/country-filter/country-filter.php:37) in /Applications/MAMP/htdocs/corpak-local/wp-includes/pluggable.php on line 1178

    Can you please help? thanks.

  12. Disable debugging in WordPress if it is enabled, in ‘wp-config.php’.

    define(‘WP_DEBUG’, false);

    Also you can do the following. In the CountyFilter plugin file. Change the line causing the notice, which is the following:

    if($_POST[‘export_data’])

    to

    if(@$_POST[‘export_data’])

  13. Thanks Sameer for getting back to me. I changed the line you suggested and the error went away so Im hoping I am good to go. However, I am still working on my local install (in use) and was wondering if I can test it from here or if I need to test it on a live site?

  14. Nevermind, I just installed on live site and it seems to be working! Cool! I didn’t seem to need to alter the code as I did on my local install either. Thanks Sameer, very excited to start using this!

  15. Hi,

    Do you have your plugin updated ? I have an error whenever i refresh the page

    Notice: Undefined index: export_data in /usr/local/pem/vhosts/user/webspace/httpdocs/oceanf.dev/wp-content/plugins/country-filter/country-filter.php on line 37 Warning: Cannot modify header information – headers already sent by (output started at /usr/local/pem/vhosts/user/webspace/httpdocs/oceanf.dev/wp-content/plugins/country-filter/country-filter.php:37) in /usr/local/pem/vhosts/user/webspace/httpdocs/oceanf.dev/wp-includes/pluggable.php on line 1178

  16. As mentioned in the comments before, In the CountryFilter plugin file, change the line causing the notice, which is the following:

    if($_POST[‘export_data’])

    to

    if(@$_POST[‘export_data’])

  17. Hi Sameer,

    Is there a reason why your template code would not work in the footer of a 404.php page? Im using it to conditionally display loops (via get_template_part()) based on IP and it works great on even page except there are no post titles displayed at all on my 404 page. Weird…

    Any ideas would be much appreciated. Thanks.

    Here is the General code but you can check my website to see what I mean.

  18. Hi!

    I bought your plugin and try to install, but error:

    Warning: require_once(geoip.inc) [function.require-once]: failed to open stream: No such file or directory in …/wp-content/plugins/country-filter/country-filter.php on line 32

    Could you help please?

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>