Keep AdSense out of your Post Preview
A while back I wrote a brief article, AdSense and WordPress 404s, which showed how to keep AdSense off your 404 error pages using WordPress' built in conditional tags.
Another concern which many users might want to address surrounds the issue of AdSense ads being served to your "Write Post" and "Write Page" sections of the WordPress back end. If you look at the pages provided to you for writing posts and pages in WordPress you'll note that a sample of the page is rendered in the "Post Preview" section at the bottom.
What's unfortunate here, is that the "post preview" pulls code right off the 'single.php' template. That means if there is any AdSense code on that template page something that can be misconstrued as a false impression can occur. While the Google AdSense ToS isn't clear on this - I'd rather be safe then sorry. You?
Anyway, here's what we do. For maximum safety we'll build off the code established in the previous post, AdSense and WordPress 404s. Please check out that post if you need more clarification, for now we're working with the following code as a starting base.
basic code
<?php if (!(is_404()) {
AdSense Code Goes Here
} ?>
In order to evade the "post preview" window we need to setup some PHP that will check the user level and stop the code from being served to users above a certain level.
That code will look something like this:
finding an author
<?php global $posts, $current_user; if($posts[0]->post_author != $current_user->ID) { ?>
AdSense Code Goes Here
<?php } ?>
The first part of this code calls the global variables containing the post information, and information on the current user (the person sitting at the computer looking at the site). The 'if' statement then checks to see if any posts have been authored by the current user. If the current user IS an author - nothing happens. If the current user isn't an author then the AdSense code will be served.
One side effect here is that if you are logged in, you won't see any ads on your blog - they will appear if you're logged out though. Is it that bad?
To finish things up just add the second block above into the first one - where it says "AdSense code goes here."
2 Responses to “Keep AdSense out of your Post Preview”
-
April 6th, 2007 at 8:37 pm
Hey Zath, glad you found it useful.
I’ve been using this code for a variety of different purposes across my themes for some time now. I use it to block all ads from appearing to me as well as for hiding any stats collecting javascript.
This makes sure that I’m not skewing my site stats and I know for certain that it’s not my visits that are being logged.
- WildBil



I’ve been wondering about this kind of this for a while now, especially after reading up about Adsense that by viewing your own ads it could mean the higher paying ones are being wasted since they don’t have a chance of being clicked.
Another very useful post Bill, thanks!
- Zath