---
title: Show Ads Only to Visitors Coming from Search Engines
date: 2008-11-02T22:10:41+00:00
modified: 2008-11-02T22:10:41+00:00
permalink: https://kaspars.net/blog/show-ads-only-to-visitors-coming-from-search-engines
post_type: post
author:
  name: Kaspars
  avatar: https://reverse.kaspars.net/gravatar/avatar/92bfcd3a8c3a21a033a6484d32c25a40b113ec6891f674336081513d5c98ef76?s=96&d=mm&r=g
post_tag:
  - How to
  - Plugin
category:
  - WordPress
---

# Show Ads Only to Visitors Coming from Search Engines

With the latest design update, I have stripped away most of the clutter in the form of secondary content. It also included removing most of the ads, just because 400 visitors a day is still to few to get more than a single click per day.

I decided to install [Ozh’s](http://planetozh.com/blog/) [*Who Sees Ads*](http://planetozh.com/blog/my-projects/wordpress-plugin-who-sees-ads-control-adsense-display/ "Who Sees Ads plugin for WordPress by Ozh") plugin to make ads visible only to [visitors coming from search engines](http://www.google.com/search?q=site%3Akonstruktors.com%2Fblog). The problem, however, is that it shows ads *only on the landing page* and not on all pages that the user visits.

**Update**: Ozh has left [a comment](#comment-8635) showing a more elegant way of achieving the same result.

To fix this, we should give a *cookie* to the arriving user and check if it still there on all the subsequent pages. Here is how you do it:

### Alter `wp_ozh_wsa_is_fromsearchengine` function to check for the cookie

```
function wp_ozh_wsa_is_fromsearchengine($doset = false) {
	global $wp_ozh_wsa;
	$ref = $_SERVER['HTTP_REFERER'];
	$yes = false;

	if (isset($wp_ozh_wsa['my_search_engines'])) {
		$SE = $wp_ozh_wsa['my_search_engines'];
	} else {
		$SE = array('/search?', 'images.google.', 'web.info.com', 'search.', 'del.icio.us/search',
		'soso.com', '/search/', '.yahoo.',
		);
	}

	foreach ($SE as $url) {
		if (strpos($ref,$url)!==false) {
			if ($doset) {
				$url = parse_url(get_option('home'));
				setcookie('wsas', 'yes', time() + 60*60, $url['path'] . '/');
			}
		}
	}

	if (isset($_COOKIE['wsas'])) {
		return true;
	}

	return false;
}
```

### Alter `wp_ozh_wsa_setcookie` to set the cookie

Place

```
$set_search = wp_ozh_wsa_is_fromsearchengine(true);
```

right after `global $wp_ozh_wsa;` in function `wp_ozh_wsa_setcookie` (line 633).

This way ads will be displayed to users coming from search engines on all of the pages they visit.