Display ads in WordPress depending on what site the visitor comes from
*original image by g-point

For some time, ThemeDigital has been displaying Adsense to search engine visitors only.

This is a great little PHP trick which makes sure I don’t bother regular visitors with ads in the sidebar, but shows them in case you arrive at Theme Digital from a list of sites I defined.

However, I wanted to show Adsense to Google visitors, and show a different banner to other visitors (while hiding Adsense).

So here’s how to do it.
Note: the first part of the tutorial was presented on CatsWhoCode – I’m just writing it here in case you’re in a hurry :)

First, we need a function that sees where the visitors are coming from.

Paste this into your functions.php file:

function scratch99_fromasearchengine(){
  $ref = $_SERVER['HTTP_REFERER'];
  $SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search', 'soso.com', '/search/', '.yahoo.');
  foreach ($SE as $source) {
    if (strpos($ref,$source)!==false) return true;
  }
  return false;
}

As you guessed it, you can add as many sites in the $SE array. Or you could just leave the search engines there.

Now we need to make it show up.

Wherever you need to show the ad in your theme, paste this code:

if (function_exists('scratch99_fromasearchengine')) {
  if (scratch99_fromasearchengine()) {
    INSERT YOUR CODE HERE
  }
}


Here’s how the above code would look if you want to show Adsense

(you have to escape from PHP to show a div):

 
<div class="widget">
 
<script type="text/javascript"><!--mce:0--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"><!--mce:1--></script></div>

That’s how I show 250×250 Adsense in the sidebar if you’re coming from a search engine.

Now what if you’re not coming from a search engine?
I decided to show a banner of the wonderful Thesis theme instead, and remove the Adsense.

The trick is very simple. After the code above, just add this:

 
<div class="widget">
<a href="http://www.yourlinkhere.com"><img title="The alt banner" src="/images/alt_banner.jpg" alt="Alt Text" width="250" height="250" /></a></div>

What it does is it check if you’re not coming from a search engine, and replaces the Adsense with whatever I specified there.

So to wrap it up, here’s the whole code I used:

 
<div class="widget">
 
<script type="text/javascript"><!--mce:2--></script>
<script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"><!--mce:3--></script></div>
 
 
 
 
 
 
<div class="widget">
<a href="http://www.yourlinkhere.com"><img title="The alt banner" src="/images/alt_banner.jpg" alt="Alt Text" width="250" height="250" /></a></div>

Of course, you can take it even further and create more functions so that each type of visitor is presented with a different banner (Google – Adsense, visitors from social networks could see a sign-up banner/form and so on)

Again, thanks to Jean-Baptiste Jung for the original recipe.