Using a location finder WP plugin like Ozh’s IP to Nation or WP-IP2Nation-Installer along with WP Super Cache

Posted by Ghatozkat on March 25, 2009

If you are trying to use a WP plugin that guesses your visitor’s country from his IP address like Ozh’s IP to Nation or WP-IP2Nation-Installer plugin along with the WP Super Cache plugin and you are yet to achieve success serving dynamic content, this blog post is for you.

WP Super Cache caches pages inside the WordPress loop on a page wise basis. So, as far as I know, you cannot prevent caching of only a certain portion of a page. This feature basically prevents you from serving dynamic content inside your blog’s pages based on your visitor’s IP address.

What’s the solution then?

The solution is using an iframe to serve the dynamic content from a page outside the WP loop.

If you are using Ozh’s IP to Nation or WP-IP2Nation-Installer plugin, you will need to add some tables from ip2nation.com or they are automatically added to the database depending upon the plugin you are using. We will be needing only those tables and all the functions that the plugin provides would be unusable as we are creating a page outside the WP loop. So my suggestion is to use the SQL script that ip2nation.com provides to insert the tables directly into your database rather than using a plugin to do the same. You can download the SQL script from here and run the SQL script via phpMyAdmin on your database server.

ip2nation.com also provides php scripts that you can use to connect to your database from the page that you will be creating outside the WP loop. I am going to show you how you can use the script provided by ip2nation.com on the page that you will use to serve dynamic content. Use the following code on your page and follow the comments on the code below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
	$server   = ''; // MySQL hostname
	$username = ''; // MySQL username
	$password = ''; // MySQL password
	$dbname   = ''; // MySQL db name
 
 
	$db = mysql_connect($server, $username, $password) or die(mysql_error());
	      mysql_select_db($dbname) or die(mysql_error());
 
	$sql = 'SELECT 
	            country
	        FROM 
	            ip2nation
	        WHERE 
	            ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'") 
	        ORDER BY 
	            ip DESC 
	        LIMIT 0,1';
 
	list($country) = mysql_fetch_row(mysql_query($sql));
  if($myvar == 'top')
  {
  ?>
  <?php
	if($country == 'us')
		{
		?>
      <!-- HTML of what you want to show your visitors from US -->
      <?php
		}
		else if($country == 'uk')
		{
		?>
      <!-- HTML of what you want to show your visitors from UK -->
      <?php
		}
		else if($country == 'ca')
		{
		?>
      <!-- HTML of what you want to show your visitors from Canada -->
      <?php
		}
		else if($country == 'au')
		{
		?>
      <!-- HTML of what you want to show your visitors from Australia -->
      <?php
		}
?>

Now save the page inside a folder in your root directory in you host (or wherever you want). For example, I have made a page named “ads.php” and put it inside the folder named “ads” in the root directory. The page can now be reached at www.someusefulinfo.com/ads/ads.php and is outside the WP loop.

Now you need to add JavaScript in your theme files to show the iframe. We can insert the iframe directly without using the JavaScript but inserting the iframe via JavaScript is better from SEO point of view. Here’s how I added the iframe in one of my theme files using js to serve the dynamic content from the ads.php file that I created outside the WP loop:

1
2
3
<script type='text/javascript'>
          document.write("<iframe src='http://www.someusefulinfo.com/ads/ads.php' width='625' height='70' scrolling='no' frameborder='0' allowTransparency='true' style='width:625px;height:70px;'></iframe>");
</script>

Some words of caution: If you are planning to show ads from ad networks based on your visitor’s IP address, my solution might not be the one to follow as certain ad networks restrict serving their ads on iframes. If you want to serve ads from ad networks depending upon where your visitor is coming from, the best solution is to deactivate the WP Super Cache plugin.

Tagged Under : ,



Make a Comment:

Popular search terms for this page:
country visitor wp plugin