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.

31 Responses

1

WordPress Plugin Releases for 07/08 | Weblog Tools Collection

July 7th, 2009 at 1:08 pm

[...] Country Filter [...]

2

EasySeo | WordPress Plugin Releases for 07/08 | SEO2Blog

July 7th, 2009 at 5:41 pm

[...] Country Filter [...]

3

New Wordpress Plugins Release 07/08/2009

July 7th, 2009 at 6:08 pm

[...] Country Filter [...]

4

WordPress Plugin Releases for 07/08 | The Cliffmaster

July 8th, 2009 at 6:39 am

[...] Country Filter [...]

5

Tim Marchant

July 12th, 2009 at 2:14 am

Thanks for this. It would be worth adding a couple of lines above telling people how to do:

} else {

:-)

6

Matt

July 17th, 2009 at 11:55 am

Hey Sameer,

Thanks for the plugin! It’s exactly what I have been looking for.

But unfortunately, it’s not working for me. I have the plugin activated, and the database installed (I see them in phpmyadmin).

I tried the following code,

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

and

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

But I see nothing at all in my post. Any ideas?

Many thanks,
Matt

7

Matt

July 17th, 2009 at 12:01 pm

Damn, the php code in my sidebar didn’t work either :/

I’m quite sure I set everything up okay.

Thanks for your help, it should be a great plugin!

8

WordPress Plugin Releases for 07/08 « Web And Graphics Design Tutorials

July 28th, 2009 at 8:56 am

[...] Country Filter [...]

9

New Useful WordPress Tricks & Hacks | ART HACKER

August 13th, 2009 at 10:48 pm

[...] Filter content by country [...]

10

30+ Tips&Plugin per Wordpress | GeekTwice

August 20th, 2009 at 1:05 am

[...] Filter Content By Country [...]

11

Unlimited New Useful Wordpress Tips,Tricks & Hacks - Themeflash : One Stop For All Your Web Resources

August 23rd, 2009 at 11:41 pm

[...] Filter content by country [...]

12

30+ New Useful WordPress Tricks & Hacks | MEN blog . net

September 4th, 2009 at 2:26 am

[...] Filter content by country [...]

13

Sergio Hernandez

September 5th, 2009 at 7:42 am

Thanks, Great plugin…! The best..

14

30+ New Useful WordPress Tricks & Hacks | My Tech way

September 14th, 2009 at 6:51 am

[...] Filter content by country [...]

15

John Thornley

October 1st, 2009 at 4:38 am

If you are from the UK (like me) then just be aware that the example is incorrect. You need to use “gb” not “uk” as is stated in the example.

Just trying this plugin out and it wasn’t working… when I investigated then this was the problem.

Apart from that, it seems like a brilliant plugin. Thank you very much for all your hard work in making it.

Cheers

John
http://www.friendsatrest.com

16

sakthi Ganesh

October 2nd, 2009 at 9:56 am

I really appreciate for these kind of exceptional plug-in . Hats off for the good work . Looking more in future

17

George Rakkos

October 13th, 2009 at 4:53 am

I second this:

“Thanks for this. It would be worth adding a couple of lines above telling people how to do:
} else {”

18

George Rakkos

October 13th, 2009 at 5:01 am

small problem i updated to 1.5 and it does not work as before

19

Leannekera

November 12th, 2009 at 7:47 am

This plugin saved my life, I really cant thank you enough!

We originally quoted for a site to be duplicated 7 times for 7 different country prices. Now all we need to do is 1 wordpress build and utilize this script to enable the site to hide/show the different prices based on ip.

I LOVE YOU FOR THIS SCRIPT!

20

Sakthi Ganesh

November 23rd, 2009 at 7:52 am

I have found some issues with my setup ( may be with this plugin)

I have blacklisted the various offensive words using the filter textbox . one of the word is “ass” .I have included the conclusion that ,the plugin may find and replace the ” Three letter world” combination of “a”,”S”,”S” that forms the word “ass”.

But what it happened is
It blocked the words that start with particular three letter word .For example it blocked and replaced theword

“Assets” with ” ***ets ” which one i didn’t expected

For example the line is become

To protect the digital ***ets( which one is gone meaningless )

Is that any solution for that ?

sameer

November 23rd, 2009 at 10:12 pm

The plugin doesn’t work as a language filter, but as a content filter for country codes.

22

Timo

November 24th, 2009 at 12:13 am

It seems that this plugin doesn’t work inside a widget :( I want to create a text box widget and insert country filter code there (to show some banners to specific country) but it just shows the country filter code there. Anyone else has the same issue?

23

Sakthi Ganesh

November 24th, 2009 at 1:12 am

Hi:

I am very sorry for mis posted comment numbered 20.

The comment numbered 20 is mis posted and these bug report should be made to the plugin

http://wordpress.org/extend/plugins/wp-content-filter/

Sammer your plugin is a exceptional plugin i will write a review on my blog and i will let you know!

I am sorry sameer and the fellow readers for this .
Thanks

24

ShocWave

December 1st, 2009 at 1:48 pm

You know what would be really cool? If it was possible to show or hide an entire post, not just its contents.

Maybe it can be done through tags or catagories?

25

Simone Carletti

December 22nd, 2009 at 10:05 pm

Hello,
there’s a small bug when the user is connecting through a proxy.

You should change

$ip_address = $_SERVER["HTTP_X_FORWARDED_FOR"])));

to

$ip_address = trim(end(split(”,”, $_SERVER["HTTP_X_FORWARDED_FOR"])));

Proxies usually appends the full ip chain in the header, values are separated by commas. The last element is the public IP.
Example

78.78.78.78 # public IP
192.168.1.1, 78.78.78.78 # proxy + public IP
192.168.1.140, 192.168.1.1, 78.78.78.78 # lan + proxy + public IP

26

James

December 28th, 2009 at 7:55 pm

Huge Thanks!

If I am able to implement this the way that I am planning (geo-specific advertising) it should bring me some extra income and if it does then I will definitely float some of that your way!

By the way, the final example above is using the wrong function. It should be the “showCountryContentInPage” in the example, correct?

sameer

December 28th, 2009 at 10:26 pm

Thanks for pointing out the error James.

28

James

December 29th, 2009 at 10:33 pm

I am very curious about Simone Carletti’s suggestion above regarding some additional parsing to handle proxied IP addresses.

Have you tested this Simone?

Sameer, what do you think?

I can tell you that I tried using my company’s VPN in a different country and the filter responded to the country of my VPN connection (not my true location), just as most web site’s do. Would Simone’s suggestion allow this code to see my originating IP address when using a VPN connection?

sameer

December 29th, 2009 at 11:52 pm

Hello James! I’ve not yet tested the proxy modification at my end, will do in a couple of days; meanwhile you can try it at your end and let me know the results.

30

plienine

January 5th, 2010 at 12:54 pm

Hello,
Is it possible to use this plugin in e107 CMS ???

Great plugin and I want to integrate it in e107 system…

Thank You for any kind of help….

sameer

January 5th, 2010 at 9:00 pm

You cannot use the plugin in e107.

Comment Form

Use the html <code> tag to insert small source code snippets

For longer code examples use http://pastie.org/.

Get latest updates by E-mail

About this blog

This site is a digital habitat of Sameer, a freelance web developer working from Pune.More

Recent Comments

  • sameer: Check to see if the 'IDE > options > format' is set to HTML. [...]
  • sameer: Google strips any newline characters form the text. Although it does accept it with the online trans [...]
  • Arjan: Fiddler is a debugging tool for IE (not Microsoft's Fiddler) [...]
  • Susan Martin: while creating a test for site, command icons on IDE greyed out and do not respond when selected. I [...]
  • Saar: Thanks for this example. helped me a lot. I have 1 problem, I am translating chunks of code, but I [...]
  • sameer: You can add extra GET variables in the options array as below: $pager_options = array( 'mode [...]
  • Martin: How can you carry over your own variables into the URL? I am using a form to POST a couple of var [...]
  • nancy: thanks very much ! first tools [...]

  • Users Online

    • 10 Users Online
    • 9 Guests, 1 Bot