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.
- 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. - 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]
Related Posts |
|

O.o bukan main hebat
June 11th, 2007 at 2:03 amwahahahahaa.. told you i dance while singing to php codes.
June 11th, 2007 at 2:08 amNice tutorial
June 11th, 2007 at 3:01 amNice…one…gonna try..it…in my blog..now…;)
June 11th, 2007 at 7:40 pmOzh: thanks!
Saravanan: hmm.. a little too early for your site don’t you think?
June 15th, 2007 at 2:01 am