How to: Display adsense to search engines visitors only

It’s a fact: People who click on your ads are people comming from search engines, not regular readers. In order to avoid being “Smart Priced” by Google Adsense, you should definitely display ads to search engines visitors only. Here’s how to do it.

First, we have to create a function. paste the code below in your theme functions.php file. Create that file if it doesn't exists.

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;
}

The $SE array is where you specify search engines. You can easily ad new search engines by adding new elements to the array.

Then, paste the following code anywhere on your template where you want your adsense ads to appear. They'll be displayed only to visitors comming from search engines results.

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

Credits goes to Stephen Cronin for this awesome recipe!

5 Responses

Feb 23 2012 16:17

Will this code slow down our website loading page?

May 01 2012 22:17

Thanks for the post; very clear and a helpful code snippet. I hear there is also a WP plugin for this; do you know about that?

May 19 2012 01:22

Is it allowed by Google (adsense) ?

Apr 08 2013 15:55

Excellent! Thanks for the code, hope it works fine. Who Sees Ads Plugin is also a useful plugin for showing ads to particular visitors. Thanks for sharing and tweeted :)

Apr 11 2013 16:15

It saved my AdSense from click bombers, thanks. :)

Trackbacks:

Leave a Comment

* Name, Email, Comment are Required

WP Theme of the week

Sponsored Likebox