Hiding Adsense from logged in users

It’s not secret, I have Google’s AdSense on my site. What, a guy has gotta make a buck. The issue is, your stats will look really misleading in the AdSense interface because every time you view the site, you’ll get an impression logged of your ads. Currently I am using the ‘AdSense Manager‘ plugin by Mutube to manage and show my ads, as well as my ‘Better AdSense Targeting‘ plugin to help dial in the ad accuracy. With 2 lines of code, we can only show ads to non-logged in users. This means that you won’t be shown your own ads when logged in.

Hiding Adsense from logged in users

Once you have the plugin installed and running, you will need to edit 1 file it is located at:

wp-content/plugins/adsense-manager/adsense-manager.php

Head down to line 222 and you will see this function: for the IF statement

01 function filter_ad_callback($matches){
02   global $_adsensem;
03   if($matches[1]==''){ /* default ad */ $matches[1]=$_adsensem['default-ad']; }
04   if(isset($_adsensem['ads'][$matches[1]])){
05     $ad=$_adsensem['ads'][$matches[1]];
06     if($ad->show_ad_here()){
07       return $ad->get_ad();
08     }
09   }
10   return '';
11 }

This is the function that shows your ads. So what we need to do here is filter out logged in users. We can do this with a function from WordPress called ‘is_user_logged_in()’. This function simply returns ‘true’ or ‘false’. So, we’ll make 2 edits.

01 function filter_ad_callback($matches){
02   global $_adsensem;
03   if (!is_user_logged_in()) { // Added logged in check
04     if($matches[1]==''){ /* default ad */ $matches[1]=$_adsensem['default-ad']; }
05     if(isset($_adsensem['ads'][$matches[1]])){
06       $ad=$_adsensem['ads'][$matches[1]];
07       if($ad->show_ad_here()){
08         return $ad->get_ad();
09       }
10     }
11   } // Close the new bracket
12   return '';
13 }

And there you have it, your plugin no longer shows AdSense Ads to logged in users. Now, any upgrade of this plugin WILL break this. I was hoping to find a hook or something in order to allow me to extend this plugin, but alas, there is nothing. I’ll be contacting the developer to hopefully get this added in the future.