Hide or Show Content Geographically in Wordpress

My first programming entry.

Would you like to hide or show special content from your viewers geographically? Would you like to show personalised content for readers in specific countries?

If you answered yes, then here’s how:

IP to Nation Plugin

First you have to get a plugin called IP to Nation (IP2Nation) Plugin from PlanetOzh. Do note that you have to also get the MySQL table from ip2nation.com.

Installation details can be found at the IP to Nation Plugin Page.

What the IP to Nation plugin does is it will check your visitor’s IP in the ip2nation.com’s mysql table and it will return your reader’s country.

Implementing Geolocation using IP to Nation Plugin

Implementing this requires you to be able to edit your theme. You can use a notepad or Dreamweaver (if you have those), and add these lines to the content you would like to hide.

Before I continue, there’s a few things to take note.

  1. You will need to replace COUNTRY with a 2 character code of the country (e.g. America = us, Australia = au, Malaysia = my). It must be in lower caps.
    Click here for a list of all the country codes.
  2. Do take note of the operator sign.
    • == means equivalent to
    • != means not equivalent to

The following code will only allow content to be displayed if the visitor matches the country stated.

<?php 
if(function_exists(”wp_ozh_getCountryCode”)){ 
  if(wp_ozh_getCountryCode()==‘COUNTRY’){
?>

   %Your_Content_Here%

<?php
  }
}
?>

The following code will only allow content to be displayed if the visitor doesn’t match the country stated.

<?php 
if(function_exists(”wp_ozh_getCountryCode”)){ 
  if(wp_ozh_getCountryCode()!=‘COUNTRY’){
?>

   %Your_Content_Here%

<?php
  }
}
?>

The following code will only allow content to be displayed if the visitor matches the country stated, otherwise it will display an alternate content.

<?php 
if(function_exists(”wp_ozh_getCountryCode”)){ 
  if(wp_ozh_getCountryCode()==‘COUNTRY’){
?>

   %Your_Content_Here%

<?php
  }
  else{

?>

   %Your_Alternate_Content_Here%

<?php
  }
}
?>

Here’s a sample of the usage:

<?php 
if(function_exists(”wp_ozh_getCountryCode”)){ 
  if(wp_ozh_getCountryCode()==‘my’){
?>

  <h2>Hello! You're From Malaysia!</h2>

<?php
  }
}
?>

You can see it in action in this blog! If you’re a visitor from outside Malaysia, you will not be able to see my Twitter badge and my chatbox. =) Test it out with a proxy!

Tell me what you think by leaving a comment!

Tagged with [tag]wordpress, geolocation, geography, ip2location, plugin, ip2nation, ip2country, geographic, content, ip, plugin, tips, blog[/tag]

Subscribe to our RSS Feed for more posts such as this via your feed reader like Google Reader

Related Posts

· Written on: 06-11-07 · 5 Comments »

5 Responses to “Hide or Show Content Geographically in Wordpress”

  1. AbbyC. wrote:

    O.o bukan main hebat

    June 11th, 2007 at 2:03 am
  2. CincauHangus wrote:

    wahahahahaa.. told you i dance while singing to php codes.

    June 11th, 2007 at 2:08 am
  3. Ozh wrote:

    Nice tutorial :)

    June 11th, 2007 at 3:01 am
  4. Saravanan07 wrote:

    Nice…one…gonna try..it…in my blog..now…;)

    June 11th, 2007 at 7:40 pm
  5. CincauHangus wrote:

    Ozh: thanks!

    Saravanan: hmm.. a little too early for your site don’t you think?

    June 15th, 2007 at 2:01 am

Leave a Reply

Note: This post is over a year old. You may want to check later in this blog to see if there is new information relevant to your comment.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word