Country Filter WordPress plugin


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 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! 

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! 

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 code="us"  display="0"] 
Buy Now for only $32

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"] 

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"] 

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="" method="post">
<input type="hidden" name="cmd" value="_xclick">
<input type="hidden" name="business" value="">
<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="" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
<img alt="" border="0" src="" width="1" height="1">

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


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.


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.


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 or to check the ip address of your mobile.

Some of the sites using the plugin

Purchase Plugin

Buy Now


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

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

  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″]
    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″]

    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″]

    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:




  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/ on line 37 Warning: Cannot modify header information – headers already sent by (output started at /usr/local/pem/vhosts/user/webspace/httpdocs/ in /usr/local/pem/vhosts/user/webspace/httpdocs/ 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:




  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( [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?

  19. Hi Sameer!

    Could you help please with this error?
    Notice: Undefined variable: ip in …\wp-content\plugins\country-filter\country-filter.php on line 212

  20. Looks like you have error display turned on in WordPress. Disable error reporting in WordPress, or add the following line in the Country filter plugin file ‘country-filter.php’ at the start.

    error_reporting( 0 );

  21. Hi Sameer. 9 months ago (february 5, 2015) I asked you if the COUNTRY FILTER PLUGUIN can show a banner only at users connected from a litle zone, like a City. You told me that this feature will be available approximately in the next 6 months. Now I contact you in order to understand if “CITY OPTION” is at this time available, or if I need wait 1, 2 or 3 months more.
    So, I need know if I will have diferent shortcodes for each banner that I will create.
    For example I need know if I can do the following two actions:
    A) Show the BANNER A in the PAGE A at the users from CITY A, using the SHORTCODE A into the PAGE A
    B) Show the BANNER B in the PAGE B at the users from CITY B, using the SHORTCODE B into the PAGE A
    Let me know. Thanks for your support.

  22. Hello,

    I’ve experimented with the City option, but with the current (free) database available it is somewhat inaccurate. We do get a accurate database but the cost of the plugin will increase substantially, which I do not want to do. So at present the city plugin option is not available. I’ll surely let you know when one becomes available.

  23. Hi sameer, thanks for your answer.
    I do one question and one suggestion.

    QUESTION: Is Maxmind a free plugin (in order to have geolocation data base)?

    SUGGESTION: When users ask you for CITY OPTION, in the 99% of de case there is no very very important absolutelly precision… Following I explain you why.
    If I want show a BANNER only for the user located in Barcelona, I am not interested in show this banner in MADRID (there are 1000 km of distance) but I am interesting in show it in other cities that are included in a radius of 50 / 100 km. So, If user want show the banner in BARCELONA it will be very happy if the banner will be shown also in BADALONA CITY, SABADELL CITY, etc because they are a cities included in a radius of 50 / 100 km. When administrator says BARCELONA (for example) it says BARCELONA because it is the big CITY (referencial city) but there is no problem in show the banner in proximals cities…
    So, I think you can use a free plugin because precision there is not important, it is more important the aproximation. For example you can say that the option is not CITY OPTION, but is CITY RADIUS OPTION or PROVINCE OPTION. For example I live in GIRONA CITY. If I use MAXIMIND it say that I am connected from “TOSSA DE MAR, GIRONA, SPAIN”. TOSSA DE MAR is an other city at 50 km of distance. But MAXIMIND also indicate the PROVINCE (GIRONA) and this province is correct. So I think is very interesting considerate PROVINCE OPTION in your COUNTRY FILTER PLUGIN.
    Do you think PROVINCE OPTION is a good option?
    Do you think PROVINCE OPTION is a solution tecnicaly possible using a free plugin?

    Let me know what you think about that (and dont forget answer me about the first question “Is Maxmind a free plugin?”

    I hope your answer. Thanks very much.
    P.D.: I dont know why, but some times your web dont let me I post a comment. It show me this error: “Error: You have entered the wrong sum in the spam protection field. Press the back button and try again.”

  24. Maxmind does not offer any freeplugins, their database is however free. The city lite database is around 30MB. I’ll play around the database and let you know.


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>