Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
WordPress Plugins
View all Guides

How to Display Ads Only to Search Engine Visitors in WordPress

Last updated on by
Follow WPBeginner on YouTube
How to Display Ads Only to Search Engine Visitors in WordPress

From our experience and research of many industry experts, it seems that Search Engine visitors are more likely to click on targeted advertisements then your regular readers. In this article, we will share a technique that will let you make more money from your blogs by simply displaying ads to your Search Engine Visitors ONLY. Whereas your regular visitors do not see these ads. This works very well with targeted Pay-Per-Click (PPC) ad units such as Google Adsense.

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:

$SE = array('/search?', '', '', 'search.', '', '', '/search/', '.yahoo.');
foreach ($SE as $source) {
  if (strpos($ref,$source)!==false) {
    setcookie("sevisitor", 1, time()+3600, "/", ""); 
function wpbeginner_from_searchengine(){
  global $sevisitor;
  if ($sevisitor==true || $_COOKIE["sevisitor"]==1) {
    return true;
  return false;

Note: In the setcookie line, be sure to change 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:

<?php if (function_exists('wpbeginner_from_searchengine')) {
  if (wpbeginner_from_searchengine()) { ?>
<?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.

We hope that you will utilize this code to increase your ad revenue. Feel free to share this article with your friends if you like it.

Source: Scratch99

Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »
  • Debajyoti Das

    Can I face a google penalty if I disable “facebook comments widget” or any widget section of my WP site for search visitors…

    PS: You can add .bing. to your code.

    • Editorial Staff

      No you don’t face penalty…

  • Kaushik Biswas

    I’ve used this for a while. It also works in blogger blogs. But the problem is, if the ads aren’t shown, then either that space has to be collapsible or some other ad or image should be shown, or else, the ad space looks blank.

  • Andrew Sylvester

    This is a really cool idea, but its biggest drawback is that it won’t work properly if you’re using any sort of WP cache plugin (as most are, or should be!).

    I took the concept and reworked it with JavaScript instead, but I wonder if there’s a better way…

    • Editorial Staff

      Care to share the JS code?

  • Abhishek Bharadwaj

    Can we show different ads to search engine and regular readers?

    • Editorial Staff

      Yes, just use the else statement in the if tag, and specify the other ads for regular readers :)

      • Sam Su

        I am not if my ads are showing. I have try this code but I don’t have other computer to preview my ads. Right now it is not showing even if I browse it coming from Google search engine.

  • Robert

    Great idea lowering impressions whilst increasing click-throughs can only help your earning potential.

    A nice simple piece of code which is easily integrated.

  • Keith Davis

    You guys know your stuff.
    If only I had your php skills!

  • LiewCF

    I don’t think modifying functions.php file is a good idea. It will be overwritten in WordPress upgrade.

    • Editorial Staff

      We are talking about theme’s functions.php which has nothing to do with the Core update.

  • Leon

    Cool idea and it works great but regular readers can make you money too!

    • Editorial Staff

      Yes, but most of the time if your user is not targeted enough for the content, then you can become a victim of Smart Pricing.

  • Saket Jajodia

    Will surly going to try this on my blogs….