Wpdaily

Daily WordPress News

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 Show Display Ads to Only Search Engine Visitors?

There are different ways to make money online, and showing display 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 display ads to search engine visitors can help boost ad revenue.

Different studies, industry experts, and our own experience shows 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 Display 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.

The post How to Display Ads Only to Search Engine Visitors in WordPress first appeared on WPBeginner.

Source