How to Display Ads Only to Search Engine Visitors

Note: The technique shown in this article can be used to display other content aside from ads that targets Search Engine visitors for your site. Perhaps a special discount code, welcome message, etc.

First thing you need to do is open your functions.php file and paste the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$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) {
    setcookie("sevisitor", 1, time()+3600, "/"".tutorialhub.com.ng");
    $sevisitor=true;
  }
}
 
function tutorialhub_from_searchengine(){
  global $sevisitor;
  if ($sevisitor==true || $_COOKIE["sevisitor"]==1) {
    return true;
  }
  return false;
}

Note: In the setcookie line, be sure to change .tutorialhub.com.ng to your own site domain.

 

Then simply open where you want to place this ad or special content (mostly in single.php however it can be any other file like sidebar.php etc), and paste the following:

1
2
3
4
<?php if (function_exists('tutorialhub_from_searchengine')) {
  if (tutorialhub_from_searchengine()) { ?>
    INSERT YOUR CODE HERE
<?php } } ?>

The code above analyzes whether the Referrer agent is from any type of Search URL which includes Google, Yahoo, Delicious, etc. If a visitor’s browser says that the Referrer Agent is from any search site that we have specified, it will store a cookie on their browser called ‘sevisitor’ for a duration of 1 hour from the time they visited your site. Most search visitors do not go on the second page, but if your site is optimized to increase pageviews or has interesting content then, they will visit additional pages. In that case, WordPress will display the Search Engine Specific Ad that you have specified to these users for the duration of one hour from the time they first visited your site. If this user bookmarks your site and comes back to it one day later because they like your site, then they will be considered your regular reader and will not see the Search Engine specific content

Be the first to comment

Leave a Reply