WPBeginner

Beginner's Guide for WordPress

  • Blog
    • Beginners Guide
    • News
    • Opinion
    • Showcase
    • Themes
    • Tutorials
    • WordPress Plugins
  • Start Here
    • How to Start a Blog
    • Create a Website
    • Start an Online Store
    • Best Website Builder
    • Email Marketing
    • WordPress Hosting
    • Business Name Ideas
  • Deals
    • Bluehost Coupon
    • SiteGround Coupon
    • WP Engine Coupon
    • HostGator Coupon
    • Domain.com Coupon
    • Constant Contact
    • View All Deals »
  • Glossary
  • Videos
  • Products
X
☰
Beginner's Guide for WordPress / Start your WordPress Blog in minutes
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

WPBeginner» Blog» Tutorials» How to Display Ads Only to Search Engine Visitors in WordPress

How to Display Ads Only to Search Engine Visitors in WordPress

Last updated on January 15th, 2018 by Editorial Staff
42 Shares
Share
Tweet
Share
Pin
Free WordPress Video Tutorials on YouTube by WPBeginner
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:

$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, "/", ".wpbeginner.com"); 
    $sevisitor=true;
  }
}
 
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 .wpbeginner.com 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()) { ?>
    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.

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

42 Shares
Share
Tweet
Share
Pin
Popular on WPBeginner Right Now!
  • How to Fix the Error Establishing a Database Connection in WordPress

    How to Fix the Error Establishing a Database Connection in WordPress

  • How to Start Your Own Podcast (Step by Step)

    How to Start Your Own Podcast (Step by Step)

  • How to Properly Move Your Blog from WordPress.com to WordPress.org

  • Checklist

    Checklist: 15 Things You MUST DO Before Changing WordPress Themes

About the Editorial Staff

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi. Trusted by over 1.3 million readers worldwide.

The Ultimate WordPress Toolkit

17 Comments

Leave a Reply
  1. fredick says:
    Mar 31, 2020 at 5:19 pm

    Why This Code Dosnt Work With Cache?

    Reply
    • WPBeginner Support says:
      Apr 1, 2020 at 8:30 am

      Your cache would show users the standard version of the site as that is what it would normally have saved for most users

      Reply
  2. Debajyoti Das says:
    Mar 3, 2011 at 2:53 am

    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.

    Reply
    • Editorial Staff says:
      Mar 7, 2011 at 9:27 am

      No you don’t face penalty…

      Reply
  3. Kaushik Biswas says:
    Feb 28, 2011 at 10:31 pm

    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.

    Reply
  4. Andrew Sylvester says:
    Feb 3, 2011 at 1:50 pm

    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…

    Reply
    • Editorial Staff says:
      Feb 3, 2011 at 4:05 pm

      Care to share the JS code?

      Reply
  5. Abhishek Bharadwaj says:
    Jan 26, 2011 at 10:57 am

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

    Reply
    • Editorial Staff says:
      Jan 26, 2011 at 4:52 pm

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

      Reply
      • Sam Su says:
        Feb 14, 2013 at 4:51 am

        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.

        Reply
  6. Robert says:
    Jan 26, 2011 at 4:50 am

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

    A nice simple piece of code which is easily integrated.

    Reply
  7. Keith Davis says:
    Jan 25, 2011 at 3:07 pm

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

    Reply
  8. LiewCF says:
    Jan 25, 2011 at 1:09 pm

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

    Reply
    • Editorial Staff says:
      Jan 25, 2011 at 1:55 pm

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

      Reply
  9. Leon says:
    Jan 24, 2011 at 6:00 pm

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

    Reply
    • Editorial Staff says:
      Jan 25, 2011 at 1:57 pm

      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.

      Reply
  10. Saket Jajodia says:
    Jan 24, 2011 at 11:06 am

    Will surly going to try this on my blogs….

    Reply

Leave a Reply Cancel reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.

Over 1,320,000+ Readers

Get fresh content from WPBeginner

Featured WordPress Plugin
All in One SEO logo
All in One SEO
Improve website SEO rankings with AIOSEO plugin. Learn More »
How to Start a Blog How to Start a Blog
I need help with ...
Starting a
Blog
WordPress
Performance
WordPress
Security
WordPress
SEO
WordPress
Errors
Building an
Online Store
Useful WordPress Guides
    • 7 Best WordPress Backup Plugins Compared (Pros and Cons)
    • How to Fix the Error Establishing a Database Connection in WordPress
    • Why You Need a CDN for your WordPress Blog? [Infographic]
    • 30 Legit Ways to Make Money Online Blogging with WordPress
    • Self Hosted WordPress.org vs. Free WordPress.com [Infograph]
    • Free Recording: WordPress Workshop for Beginners
    • 24 Must Have WordPress Plugins for Business Websites
    • How to Properly Move Your Blog from WordPress.com to WordPress.org
    • 5 Best Contact Form Plugins for WordPress Compared
    • Which is the Best WordPress Popup Plugin? (Comparison)
    • Best WooCommerce Hosting in 2020 (Comparison)
    • How to Fix the Internal Server Error in WordPress
    • How to Install WordPress - Complete WordPress Installation Tutorial
    • Why You Should Start Building an Email List Right Away
    • How to Properly Move WordPress to a New Domain Without Losing SEO
    • How to Choose the Best WordPress Hosting for Your Website
    • How to Choose the Best Blogging Platform (Comparison)
    • WordPress Tutorials - 200+ Step by Step WordPress Tutorials
    • 5 Best WordPress Ecommerce Plugins Compared
    • 5 Best WordPress Membership Plugins (Compared)
    • 7 Best Email Marketing Services for Small Business (2020)
    • How to Choose the Best Domain Registrar (Compared)
    • The Truth About Shared WordPress Web Hosting
    • When Do You Really Need Managed WordPress Hosting?
    • 5 Best Drag and Drop WordPress Page Builders Compared
    • How to Switch from Blogger to WordPress without Losing Google Rankings
    • How to Properly Switch From Wix to WordPress (Step by Step)
    • How to Properly Move from Weebly to WordPress (Step by Step)
    • Do You Really Need a VPS? Best WordPress VPS Hosting Compared
    • How to Properly Move from Squarespace to WordPress
    • How to Register a Domain Name (+ tip to get it for FREE)
    • HostGator Review - An Honest Look at Speed & Uptime (2020)
    • SiteGround Reviews from 4196 Users & Our Experts (2020)
    • Bluehost Review from Real Users + Performance Stats (2020)
    • How Much Does It Really Cost to Build a WordPress Website?
    • How to Create an Email Newsletter the RIGHT WAY (Step by Step)
    • Free Business Name Generator (A.I Powered)
    • How to Create a Free Business Email Address in 5 Minutes (Step by Step)
    • How to Install Google Analytics in WordPress for Beginners
    • How to Move WordPress to a New Host or Server With No Downtime
    • Why is WordPress Free? What are the Costs? What is the Catch?
    • How to Make a Website in 2020 – Step by Step Guide
Deals & Coupons (view all)
LearnDash
LearnDash Coupon
Get the lowest price on the best learning management system (LMS) plugin for WordPress.
AWeber logo
AWeber Coupon
Get a completely FREE plan with AWeber, or 14.9% off the Pro plan. Start your email list today.
Featured In
About WPBeginner®

WPBeginner is a free WordPress resource site for Beginners. WPBeginner was founded in July 2009 by Syed Balkhi. The main goal of this site is to provide quality tips, tricks, hacks, and other WordPress resources that allows WordPress beginners to improve their site(s).
Join our team: We are Hiring!

Site Links
  • About Us
  • Contact Us
  • FTC Disclosure
  • Privacy Policy
  • Terms of Service
  • Free Blog Setup
  • Free Business Tools
Our Sites
  • OptinMonster
  • MonsterInsights
  • WPForms
  • SeedProd
  • Nameboy
  • RafflePress
  • Smash Balloon

Copyright © 2009 - 2021 WPBeginner LLC. All Rights Reserved. WPBeginner® is a registered trademark.

Managed by Awesome Motive | WordPress hosting by SiteGround | WordPress CDN by MaxCDN | WordPress Security by Sucuri.