Beginner's Guide for WordPress / Start your WordPress Blog in minutes

How to Display Ads Only to Search Engine Visitors in WordPress

Do you want to show targeted ads to only visitors from search engines?

From our experience and the research of many industry experts, it seems that search engine visitors are more likely than your regular readers to click on targeted advertisements. By showing ads only to these visitors, you can boost the click-through rate (CTR) and increase sales.

In this article, we will show you how to display ads only to search engine visitors in WordPress.

How to display ads only to search engine visitors in WordPress

Why Display Ads to Only Search Engine Visitors?

There are different ways to make money online, and showing ads is one of them.

You can use Google AdSense to show ads on your WordPress blog and earn a set fee when a user clicks on the advertisements. This strategy is called cost-per-click (CPC).

However, getting more clicks can be a challenge if the ads aren’t targeted to the right audience. This is where limiting ads to search engine visitors can help boost ad revenue.

Different studies, industry experts, and our own experience show that visitors from search engines are more likely to click on ads on your site compared to other visitors. You can show the right ads to the right users and improve CPC.

This strategy also helps show ads only when they are needed. Having too many advertisements can be distracting and bad for the user experience. By displaying them to only search engine visitors, your WordPress website won’t be cluttered with ads.

That said, let’s see how you can display ads to only search engine visitors.

Showing Ads to Only Search Engine Visitors

To display ads to only visitors from search engines, you will need to add a custom code snippet to your WordPress website.

This might sound technical and difficult, but we will show you an easy way to add code snippets without editing code or hiring a developer.

If you haven’t set up ads on your site, then please see our guide on how to properly add Google AdSense to WordPress.

Next, you will need to install and activate the WPCode plugin. To learn more, please see our guide on how to install a WordPress plugin.

WPCode is the best code snippet plugin for WordPress, and it helps you insert custom code anywhere on your site. It also helps you manage and organize all your code snippets.

Note: For this tutorial, we will use the WPCode Lite version, which is available for free. However, there are premium plans that offer more features like conditional logic, safe error handling, a code snippets library, and more.

Upon activation, you need to head to Code Snippets » + Add Snippet from your WordPress dashboard. Next, click the ‘Add Your Custom Code (New Snippet)’ option.

Add custom CSS snippet

From here, you will need to copy this code snippet:

$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.

Next, you must paste the code into the WPCode ‘Code Preview’ area. You will also need to enter a name for your snippet and then click the ‘Code Type’ dropdown menu and select the ‘PHP Snippet’ option.

Enter custom code for search engine visitors

After that, you will need to scroll down and select the Insertion method for the code snippet.

WPCode will use the ‘Auto Insert’ option by default and run the code everywhere. However, you can change this and insert the custom code on specific pages, before or after content, show it on eCommerce pages, and more.

Edit insertion method for code

As an alternative, you can also switch to the ‘Shortcode’ insertion method and manually enter a shortcode to run the code snippet.

For this code snippet, we recommend using the Auto Insert method.

Once you are done, don’t forget to click the toggle at the top to activate the code snippet, and then click the ‘Save Snippet’ button.

Activate and save ad code in WPCode plugin

Choose Where to Display Ads on Your Site

Next, you will need to add another code snippet and choose where you’d like to display the ads to only search engine users.

Simply copy the following code:

<?php if (function_exists('wpbeginner_from_searchengine')) {
  if (wpbeginner_from_searchengine()) { ?>
    INSERT YOUR CODE HERE
<?php } } ?>

Note: Don’t forget to replace ‘INSERT YOUR CODE HERE’ in the above snippet with your Google AdSense code.

The snippet above uses the first code as a reference and analyzes whether the referrer agent is from any type of search URL, which includes Google, Yahoo, Delicious, and more.

If a visitor’s browser says that the referrer agent is from any search site that you have specified, then it will store a cookie on their browser called ‘visitor’ for 1 hour from the time they visited your site.

To add the code, simply go to Code Snippets » + Add Snippet from your WordPress dashboard and select the ‘Add Your Custom Code (New Snippet)’ option.

Add custom CSS snippet

Next, you can enter a name for your code snippet at the top and paste the code into the ‘Code Preview’ area.

You will also need to change the ‘Code Type’ by clicking the dropdown menu and selecting the ‘PHP Snippet’ option.

Enter code and select code type

After that, you can click the ‘Save Snippet’ button and scroll down to the Insertion section.

Here, you will need to select the ‘Shortcode’ method. This way, you can easily add the shortcode to show display ads anywhere on your site.

Add shortcode for display ads

You can copy the shortcode or write it down in a notepad file.

When you are done, don’t forget to click the toggle at the top to activate the code and then click the ‘Update’ button.

To add the shortcode, you can head to any section of your website. For example, if you want to show banner ads to search engine users in the sidebar, then just go to Appearance » Widgets from the WordPress dashboard.

From here, you can click the ‘+’ button to add a Shortcode widget block to the sidebar area.

Add a shortcode widget block

Go ahead and enter the shortcode you just copied. Once you are done, simply click the ‘Update’ button.

WordPress will now display the search engine-specific ads that you have chosen to these users for a total 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 content, then they will be considered your regular reader and will not see the search engine-specific ads.

We hope this article helped you learn how to display ads only to search engine visitors in WordPress. You may also want to see our ultimate guide to WordPress SEO and our expert picks for the best WordPress ad management plugins.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

23 CommentsLeave a Reply

  1. Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. It sounds very good, to reward regular website visitors by offering an ad-free experience when they come directly, while showing ads to visitors from search engines. Such a win-win solution, where one can pamper regular users a bit more and essentially give them a certain bonus. It’s a great idea to display ads based on the referrer and where they came from, although I haven’t implemented Google Ads on the website yet. Nonetheless, I’ll definitely consider this approach, especially in terms of the benefit it brings to regular users.

  3. This is really interesting idea! Thanks to this method we can potentially lower the number of impressions and at the same time increase CTR. At least in theory…
    Can this affect Google Ads in such way it will increase our CPS and/or CPM and make us more money?

  4. Would be great setting a shortcode for this, then you can set it on a widget or in any post/page…

    Nice piece of code brow!

    • We’ll certainly consider a method for that the next time we review this code :)

      Admin

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

      Admin

  5. 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.

  6. 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.

  7. 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…

      • 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.

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

    A nice simple piece of code which is easily integrated.

Leave A 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.