How to Hide AdSense in WordPress Previews

Posted by Lasantha Bandara File Under : adsense, wordpress

How to Hide AdSense in WordPress Previews

When you preview posts or pages in the WordPress editor, AdSense ads (and other ad scripts) may also load. This is unnecessary and can make previews slower or distracting. Luckily, WordPress provides a simple way to stop ads from loading on preview pages.

Why Hide AdSense in Previews?

  • Faster previews: Preview pages load more quickly without ad scripts.
  • Cleaner editing: You can focus on writing without distractions from ads.
  • No accidental clicks: Prevents useless impressions or risky clicks while editing.

How to Hide AdSense in Previews?

Wrap your ad code inside a conditional check using the WordPress function is_preview(). Here's the code:

<?php if ( !is_preview() ) : ?>

<!-- Place your AdSense or other ad code here -->

<?php endif; ?>

How It Works?

The function is_preview() returns true whenever you are looking at a preview URL. For example:

https://www.bloggertipandtrick.net/?p=7476&preview=true

With the code above, ads will not load on preview URLs, but they will appear normally on live posts, pages, and your homepage.

Tip: If you also want to hide ads for logged-in admins and editors, you can combine !is_preview() with !current_user_can('edit_posts').

Conclusion

By adding a small conditional check, you can keep your WordPress previews fast, clean, and ad-free. It only takes one line of code, and it makes editing much more comfortable.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.