How to Manage Content You See in WordPress Preview Mode

If you frequently use WordPress to publish content, you’re probably familiar with Preview mode. This handy feature allows you to review your posts as you write them or perform a final check before publishing. But did you know that you can do even more in this mode?

WordPress includes a is_preview tag that offers flexibility for adding or removing content and code snippets when viewing content in Preview mode. You can display notifications or hide specific content using simple code examples like the ones we’ll explore below.

60+ Most Wanted WordPress Tricks and Hacks (Updated)

60+ Most Wanted WordPress Tricks and Hacks (Updated)

Have you ever came across a WordPress blog, saw something you liked, and thought; how they did that,... Read more

How to Display Notifications in Preview Mode

Let’s look at a practical use of the is_preview tag. By default, WordPress doesn’t indicate that you’re in Preview Mode. With this tag, we can create a notification to make this clear.

Here’s how you can add a notification in the header.php file of your theme:

<?php if (is_preview()) : ?>
<div class="alert alert-info">
  <strong>Note:</strong> You are previewing this post. This post has not yet been published.
</div>
<?php endif; ?>

Enhance the notification’s appearance with some custom CSS to ensure it stands out effectively.

Preview Mode Notification in WordPress

How to Hide Ads and Analytics in Preview Mode

If you use AdSense or Google Analytics, hiding these elements in Preview Mode can prevent accidental ad clicks (which violate Google AdSense TOS) and exclude your own visits from analytics tracking. This is also beneficial for ads that count page views, as it ensures more accurate metrics for advertisers.

To hide ads during Preview Mode, use this code:

<?php if (!is_preview()) : ?>
  // Ads Code
<?php endif; ?>

To manage Google Analytics, insert this code into your theme’s header.php or footer.php:

<?php if (!is_preview() && !is_admin()) : ?>
  // Google Analytics Code
<?php endif; ?>

We’ve also included the is_admin tag to ensure that Google Analytics is disabled when logged in as an Administrator.

We hope these tips will help you make the most of Preview Mode in WordPress. If you have any questions or need further assistance, feel free to ask in the comments section below.

WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail