From our years of hands-on experience optimizing WordPress sites for maximum monetization—and backed by insights from top industry experts—we've found that search engine visitors click PPC ads at higher rates than regular readers. In this proven guide, we'll walk you through a simple technique to display ads exclusively to search traffic, increasing your earnings without frustrating loyal users. It pairs perfectly with networks like Google AdSense.
Note: Adapt this for other targeted content, such as special discount codes or personalized welcome messages.
First, open your theme's functions.php file and add this code:
<?php
$ref = $_SERVER['HTTP_REFERER'];
$SE = array('/search?','Images.google.','Web.info.com','search.','Del.icio.us/search','soso.com','/search/','.yahoo.');
$sevisitor = false;
foreach($SE as $source){
if(strpos($ref, $source) !== false){
setcookie("sevisitor", 1, time() + 3600, "/", ".yourdomain.com");
$sevisitor = true;
break;
}
}
function wpbeginner_from_searchengine(){
global $sevisitor;
if($sevisitor || (isset($_COOKIE["sevisitor"]) && $_COOKIE["sevisitor"] == 1)) return true;
return false;
}
?>Important: In the setcookie line, replace .yourdomain.com with your site's domain. Always back up your site before editing files.
Next, in your template file (like single.php or sidebar.php), insert your ad or content code like this:
<?php if(function_exists('wpbeginner_from_searchengine') && wpbeginner_from_searchengine()){ ?>
YOUR AD OR CONTENT CODE HERE
<?php } ?>This script detects search engine referrers (Google, Yahoo, Delicious, etc.) via the HTTP_REFERER header. If matched, it sets a 'sevisitor' cookie lasting 1 hour. Regular readers or returning bookmarked users won't see the ads. Most search visitors bounce quickly, but if they explore further, they'll still see your targeted content during that window.
We've used this reliably across numerous sites to lift ad performance. Give it a try to enhance your revenue—and share if it helps!