Display ads in WordPress depending on what site the visitor comes from

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.

    Please share it if you liked it:



Related Posts

  1. WordPress Ecommerce Plugin – PHPurchase
  2. WordPress Membership Plugins and Resources
  3. WordPress Ecommerce Plugins, Themes and Tutorials
  4. How to remove title tags in WordPress
  5. Win WordPress Hosting and ElegantThemes subscription

3 Comments For This Post

  1. grife bolsas

    Loved reading this post, do you also have some sort of newsletter?

  2. grife bolsas

    Sorry for the double post, but i\’m trying to read this blog through RSS and its not working – any ideas?

  3. ThemeDigital

    @grife bolsas – I’ve checked the feed, and it seems to be working fine.

    What browser are you using?

1 Trackbacks For This Post

  1. uberVU - social comments

    Social comments and analytics for this post…

    This post was mentioned on Twitter by themedigital: Display ads in WordPress depending on what site the visitor comes from http://ow.ly/Kd84 #wordpress #php…

What do you think of this?