Lunacy Unleashed

Notes from the field in the War on Spam

AdSense on WordPress 2.0

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.

December 27, 2005 - Posted by | AdSense, Advertising, Google, WordPress, WordPress 2.0

40 Comments

  1. […] Lunacy Unleashed io error outlines a handy new template tag, is_preview, and how to use it to prevent showing your adsense ads in the live preview, and potentially pissing off google. // Used for showing and hiding user information in the comment form function ShowUtils() { document.getElementById(“authorinfo”).style.display = “”; document.getElementById(“showinfo”).style.display = “none”; document.getElementById(“hideinfo”).style.display = “”; } function HideUtils() { document.getElementById(“authorinfo”).style.display = “none”; document.getElementById(“showinfo”).style.display = “”; document.getElementById(“hideinfo”).style.display = “none”; } […]

    Pingback by AdSense on WordPress 2.0 at WordPress Station | December 27, 2005

  2. wow. Potentially serious indeed, thanks for sharing this.

    Comment by Ozh | December 27, 2005

  3. Ozh, I caught this very early in alpha testing and I’d requested (it’s in trac somewhere) a template tag for this very reason. is_preview() is the result.🙂

    Comment by Michael Hampton | December 27, 2005

  4. […] Michael Hampton(?是 Bad Behavior plugin 的作者)剛剛在他的 WordPress.com blog 內??出一個在 WordPress 2.0 內使用 Google AdSense 注?之事,文章??? AdSense On WordPress 2.0。 […]

    Pingback by 天佑的自由天地 » Blog Archive » 在 WordPress 2.0 使用 AdSense 注?之事 | December 28, 2005

  5. Thanks. But I test it in my blog, it seems that the tag doesn’t work, and Ads is still in the post preview. So why?

    Comment by ChamPioNeer | December 28, 2005

  6. […] Zweiter Knackpunkt ist das automatische Preview. Ich will es nicht. Ich mchte gern entscheiden wann ich ein Preview sehen will und nicht bei jedem Zwischenspeichern. Zudem macht es Probleme mit Adsense und vermutlich auch den Statistiken. Es gibt zwar fr Adsense ein Workaround aber diese Unbequemlichkeit htte nicht sein mssen. Ich habe leider keinen Weg gefunden das Zwangspreview abzuschalten. […]

    Pingback by Das Blogamt » Wordpress 2.0 Erster Eindruck oder “never touch a running system” | December 28, 2005

  7. If that’s happening, then it is called a bug, and you should report it.

    Comment by Michael Hampton | December 28, 2005

  8. Sorry, I can’t login with my forum username, so can’t report this bug.

    Comment by championeer | December 29, 2005

  9. […] Other then talking about the wordpress 2.0 trackback issue, another thing which pop in my eyes is Adsense on WordPress 2.0 […]

    Pingback by Ah Knight’s Blog » Wordpress 2.0 and Adsense | December 30, 2005

  10. […] 刚刚在天佑的自由空间上看到,如果你的Blog中使用了Google Adsense,那么Wordpress 2.0新增加的贴?预览(Post Preview)功能?能会导致Google?用你的Ads账? 。马上顺藤摸瓜,去天佑引用的原帖中去看,原?是 这样的: […]

    Pingback by ChamPioNeer^?片泥 » Blog Archive » Wordpress 2.0 的Google Adsense问题 | December 31, 2005

  11. […] In my previous post I wrote about a problem I had with some javascript code that is placed inside the <head> HTML tags of my header.php file. There was a conflict between this small piece of code and the live preview feature of the WP editor, so no editing was possible at all for my posts. Furthermore, this feature could possibly get Google Adsense users into trouble. Michael Hampton has written an explanatory article about this issue. […]

    Pingback by A temp solution for the is_preview WP issue « Raoul’s Land Reloaded! | January 1, 2006

  12. Maybe it is due to conflict with the build-in cache mechanism.

    Comment by yan | January 3, 2006

  13. […] 因为首先?到的是这篇文章:AdSense on WordPress 2.0,大体的中文翻译在这儿:在 WordPress 2.0 使用 AdSense 注?之事。讲 在 WordPress 2.0 中有一個很酷的功能??? Post Preview,?是在 edit / view draft 時,在文章下方會顯示一個 preview 畫?,內裡會顯示這篇文章在發表時的樣?,但如果有使用 Goolge AdSense,當 preview 時都會直接讀? Google AdSense 廣告,但因為文章還未發表,如果跟著 Google 嘗試尋找這篇文章時,就會出? 404 Not found 的情?,有?能會被 Google 暫?你的 AdSense 戶?。 […]

    Pingback by 桑林志 » is_preview() 问题 | January 3, 2006

  14. […] It seems that the problem is the combination of adsense and WordPress 2.0 preview. By tracking the traffic from my PC and the web access log, the adsense tricks the page to be crawled. Then I found this post about WordPress 2.0 and AdSense.  It looks like until is_preview is fixed, don’t put adsense on the WordPress blog. […]

    Pingback by EUGENIUS » Yahoo! MySQL hiccups … | January 8, 2006

  15. WordPress 2.0 problems for google adsense users, and a workaround

    As just another reason to not upgrade to WordPress 2.0 (or even install it in the first place for now), it’s been widely reported that WP 2.0’s live preview feature causes serious problems for those bloggers who utilize Google’s Adsense

    Trackback by How To Blog | January 10, 2006

  16. Thank you for the information.

    Comment by valentino | January 13, 2006

  17. […] Solution has been provided by the Creator of bad behavior plugin. You have to add this code around your AdSense code: if (!is_preview()): ?> // Paste your AdSense code here // endif; […]

    Pingback by blog.IT » Wordpress 2.0 and Adsense problems | January 15, 2006

  18. […] As is evident from looking at the sidebar to the right of this text, I am using the ‘Top 10 posts and Views per post’ WP plugin by Mark Ghosh. I am very happy with the “viewer popularity” rating this plugin provides, but I have been a little annoyed with my own page views being included in the view counts. Due to Adsense concerns, I have disabled the post preview in the WP post editor. This means that I tend go back and forth between viewing and editing a post – something which would capsize the view count. […]

    Pingback by gersbo.dk » Blog Archive » Modification of ‘Top 10 Posts’ plugin | January 19, 2006

  19. Is it possible that this issue has ben fixed here?

    Comment by Marty | January 24, 2006

  20. Hi there,

    just my 2 cents, from a fresh WordPress 2.0 install:

    The preview feature puts a wordpress page with the posting in a frame. When I open this page in its own window (Firefox: “open frame in new tab/page”)… the unpublished page does NOT produce a 404, just a “HTTP/1.x 200 OK” (tested with Firefox Live HTTP Headers).

    So Google does not get a 404, but the page as you are drafting it. One still can argue that this leads to badly targeted ads since Google will scan the page in its early stage (when there are just a few words on first save, not the entire final text). This is however NOT a problem when you use URL rewriting, because the framed draft page will have a url of the form /?p=xx.
    It only gets its “slug” and final url after publishing eg /2006/01/29/test-test/
    So the badly targeted ads at /?p=xx do not matter (since they’re not visible!), and the final post at /2006/01/29/test-test/ will only
    be accessible to Google when it’s published – so when it’s accessible to everybody.

    Please correct me if I’m wrong🙂

    Comment by Pascal Van Hecke | January 29, 2006

  21. Oh BTW: the Regulus Theme at WordPress.com here doesn’t have named anchors for individual comments…
    https://error.wordpress.com/2005/12/27/adsense-on-wordpress-20/#320 doesn’t point to any particular comment – I know that’s not your fault, just wanted you to know🙂

    Comment by Pascal Van Hecke | January 29, 2006

  22. Pascal, you’re wrong. Google does indeed get a 404, because unlike you, it isn’t logged in as you and thus can’t see your draft. Not to mention that it appears in a framed page, so all sorts of other things go haywire.

    Comment by Michael Hampton | January 30, 2006

  23. ah, IC!
    Sorry I overlooked the fact I was logged in!

    In case of (one of) my blogs, the adsense code comes with the posting, so Google gets a HTTP referer from a page where it doesn’t detect any Adsense code, I assume that could be a reason to have the account suspended (you’re not allowed to put adsense code behind logins).

    thx for the posting and the correction!

    Comment by Pascal Van Hecke | January 30, 2006

  24. […] Lunacy Unleashed has a great article on dealing with the adsense problems in WordPress 2.0 dealing with the adsense problems in WordPress 2.0, as well as a few workarounds. […]

    Pingback by WordPress 2.0 problems for google adsense users, and a workaround | How to Blog | January 30, 2006

  25. […] WordPress 2.0 has a preview feature that can cause Adsense problems: […]

    Pingback by Google Adsense and Wordpress 2.0: beware of the preview | January 31, 2006

  26. I think Google gets a 404 error, but the bot finds no ads, so there can be no violation of TOS. No ads on page, no violation.

    The fact that the Google bot got a 404 error is not relevant. I have posted a short discussion on my site.

    Comment by Mr Blogmeister | January 31, 2006

  27. Maybe you’d care to share the URL with us? Or, Gawd forbid, write it up, link here and pingback?

    Comment by Michael Hampton | January 31, 2006

  28. Sorry, I included URL with above post, but somehow got stripped. Basically, I contend that the problem is not limited to WordPress 2.0, but has been around since we’ve been able to open local HTML files with our browser. Or, since Adsense was introduced. I don’t think it’s a problem.

    The URL in case it gets stripped again is http://mrblogmeister.com

    Larry

    Comment by Mr Blogmeister | January 31, 2006

  29. Then you’re still wrong.🙂 This problem is quite different from the localhost issue. Google knows about the localhost/local file issue and doesn’t care, so long as you aren’t clicking on the nice attractive ads that appear. They DO care about 404s.

    Comment by Michael Hampton | January 31, 2006

  30. Like everyone else they may not like encountering a 404 page, but their TOS does not address that issue. Their TOS addresses only whether non-content pages have adsense ads.

    Michael, I am willing to concede that Google knows about the localhost issue, and they aren’t neophytes with 404 page errors either. But the question is “Have we placed ads on the 404 page that Google sees?” The answer is NO. According to TOS, no ads, no violation. Simple as that..

    If you still object because Google is getting a 404 with no content, we could create a custom 404 page with content. Naturally, we still wouldn’t place ads on it. How could that then possibly violate Google TOS?

    Comment by Mr Blogmeister | January 31, 2006

  31. Hi,

    I think the rule that applies here is: you shouldn’t place ads on pages that are hidden behind a login.
    Which is the case for the draft posts, as only the logged-in user can see them.

    Comment by Pascal Van Hecke | February 1, 2006

  32. Hm, it’s obvious you haven’t actually read the terms carefully, so you should go do that first before posting further on this issue. Especially if you intend to keep your AdSense account.🙂

    Comment by Michael Hampton | February 1, 2006

  33. Michael, I don’t know whether the above was directed to me or Pascal, but in 3 posts you have said nothing but “you are wrong” I have carefully read the Adsense TOS and the Program Policy. Where in your CAREFUL reading do you find that it violates TOS if the adsense robot encounters an error page.

    I have found only the following ” (v) display any Ad(s), Link(s), or Referral Button(s) on any error page, on any registration or “thank you” page……”

    Where are you reading differently? Or is it a matter of your interpretation? Please don’t simply claim that everyone is wrong. If we are wrong, enlighten us. I assume you know that the TOS was modified 11/04. Is your careful reading up-to-date?

    Comment by Mr Blogmeister | February 2, 2006

  34. WordPress 2.0?Google Adsense

    WordPress2.0?ら投稿画???スト・プレビュー機能???????。
    投稿後?画??プレビュー???る便利?機能????Google Adsense???アフィリェイト広告??をシングルページ??置?…

    Trackback by chucksville | February 5, 2006

  35. Is it possible to add adsense words to a WordPress.com account without putting them in any specific post.

    Comment by theSTYLES | February 13, 2006

  36. […] More info can be found here […]

    Pingback by AdSense Money, Blog » WordPress 2.0 can close your AdSense account | March 7, 2006

  37. […] 因为首先?到的是这篇文章:AdSense on WordPress 2.0,大体的中文翻译在这儿:在 WordPress 2.0 使用 AdSense 注?之事。讲 在 WordPress 2.0 中有一個很酷的功能??? Post Preview,?是在 edit / view draft 時,在文章下方會顯示一個 preview 畫?,內裡會顯示這篇文章在發表時的樣?,但如果有使用 Goolge AdSense,當 preview 時都會直接讀? Google AdSense 廣告,但因為文章還未發表,如果跟著 Google 嘗試尋找這篇文章時,就會出? 404 Not found 的情?,有?能會被 Google 暫?你的 AdSense 戶?。 […]

    Pingback by Three Bears » Blog Archive » WP2.0 预览问题 | March 15, 2006

  38. […] More about the AdSense issue can be found here and here. […]

    Pingback by The Social Programmer » Blog Archive » Upgraded to WordPress 2.02 | March 21, 2006

  39. […] More about the AdSense issue can be found here and here. […]

    Pingback by The Social Programmer » Blog Archive » Test | March 21, 2006

  40. http://www.bloggen.be/fordik rolex replica rolex replica

    Comment by xz | September 28, 2006


Sorry, the comment form is closed at this time.

%d bloggers like this: