If you’re upgrading to WordPress 2.0 and use Google AdSense, there is something very important you need to know to ensure you continue to get well-targeted ads, and that Google doesn’t suspend your account for program violations.
One of the new features in WordPress 2.0 is a live post preview. If you scroll to the bottom of the page while editing a post, you’ll see a live preview of how your page will look once it’s published. This is a very nice addition to WordPress, but for AdSense publishers, and those using other context-targeted ad networks, it presents a serious problem.
When the post preview is rendered, it will try to fetch your Google ads!
And because the post hasn’t been published yet, when Google’s bot tries to crawl the page a few seconds later, it will receive a 404 error.
The best case scenario for this is that some of you (who don’t use permalinks) will receive very poorly targeted ads for up to two weeks after you publish your post.
And the worst case scenario, since Google prohibits displaying ads on 404 pages, is that you could get your account suspended.
WordPress 2.0 does provide a solution, though; it’s the new is_preview template tag. This new tag tells whether the post is being displayed in the post preview section while it’s being edited.
So all you need to do is to add in a check for this into your template code wherever you have placed AdSense, and the problem will be solved. Just add this code around your AdSense code:
<?php if (!is_preview()): ?>
// Paste your AdSense code here //
<?php endif; ?>
This way, the post preview will not try to show Google ads, and they will only be shown once your post is published. This will keep your AdSense account safe and your ads well-targeted.
Update: It’s come to my attention that is_preview() may be broken. If you find that’s the case, submit a bug report and post the ticket number in the comments below so we can track it.
Update: I’ve tested is_preview() and it seems to be working just fine. Like other template tags, it only works inside the loop, though.
Update: Since people frequently place ads outside the loop, there needs to be a way to test for this outside the loop. The following workaround worked for me:
<?php global $wp_query; if (!$wp_query->is_preview): ?>
// Paste your AdSense code here //
<?php endif; ?>
Update: Ticket 2188 is open for is_preview() acting strangely.