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 Who Sees Ads plugin to make ads visible only to visitors coming from search engines. 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 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;
	$yes = false;

	if (isset($wp_ozh_wsa['my_search_engines'])) {
		$SE = $wp_ozh_wsa['my_search_engines'];
	} else {
		$SE = array('/search?', '', '', 'search.', '',
		'', '/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


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


  1. Ozh says:

    The reason I wrote it this way is: on the landing page, a search engine visitor is… a search engine visitor. One of those 80% that will go away right now because in a glance they won’t find what they were looking for, and hopefully go away with an ad click.

    Now, if they visit a second page, they become a LOT more than just a one time search engine visitor. They got interested by your CONTENT, so why pollute their read with ads that they won’t click anyway?

    This said, there is a much better way to do what you’re wanting than modify my plugin.

    1) Create a custom function that either sets a cookie for SE visitors or checks if the cookie exists
    Something like:
    function cookie_or_SE() {
    if (wp_ozh_wsa_is_fromsearchengine())
    [set the cookie];
    if ([the cookie])
    return true;
    return false;
    2) In your ad rules, use an advanced rule like:
    if (cookie_or_SE()) then {display}

  2. Kaspars says:

    Ozh, the method you showed is much more elegant indeed.

    Your point about the visitors coming from search engines and finding value in the content is also very true. I hadn’t thought about it that way. All I knew was that someone has to see those ads, and I don’t want them to be my regular readers.

    It is also interesting to see that the number of clicks has gone up by 50% since implementing the change.

  3. gw says:

    You said “It is also interesting to see that the number of clicks has gone up by 50% since implementing the change.”

    your original change to add the Ozh plugin or after your refinement to give them ads on all pages ?


    • Kaspars says:

      gw, the click rate has’t really changed since I installed the plugin or made the changes described above. It went up only for a day or two and then got back to the normal 0.5 clicks per 400 visitors.

  4. Oscar says:

    Thanks for the informative article. Very useful to know how to do this.

    Do you have any idea or a pointer as to how we might integrate this with existing ad plugins?

    like this one for example: Random / Rotating Ads Plugin for WordPress — by datafeedr? you can google it or it is here:

    oh, btw, the tab order on your comment form seems to be out of order. if I tab after the e-mail field I get to “submit comment” instead of “notify me of follow up comments” just an fyi, but maybe you want it that way. Cheers.

Leave a Reply