# "Preview" Banner on Unpublished Content

> Shows a clear banner at the top of the page whenever someone previews a draft, pending or scheduled post, so reviewers never mistake a preview for the live page or share it by accident.

- Type: PHP
- Runs: Frontend only
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: preview, drafts, editorial
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/content-preview-banner/

## Code

```php
add_action( 'wp_body_open', function () {
    if ( ! is_preview() || ! is_user_logged_in() ) {
        return;
    }

    $post   = get_post();
    $status = $post ? get_post_status_object( $post->post_status ) : null;
    $text   = 'Preview: this page is not published yet';
    if ( $post && 'future' === $post->post_status ) {
        $text = 'Preview: scheduled for ' . get_the_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $post );
    } elseif ( $status && 'publish' === $post->post_status ) {
        $text = 'Preview of unsaved changes';
    } elseif ( $status ) {
        $text = 'Preview: ' . $status->label;
    }

    printf(
        '<div role="status" style="position:sticky;top:0;z-index:99997;padding:10px 16px;background:#dba617;color:#1d2327;font:600 14px/1.4 system-ui,sans-serif;text-align:center">%s%s</div>',
        esc_html( $text ),
        $post && current_user_can( 'edit_post', $post->ID ) ? ' &middot; <a href="' . esc_url( get_edit_post_link( $post->ID ) ) . '" style="color:inherit">Back to editing</a>' : ''
    );
} );
```

## Notes

Only shown to logged-in users viewing a preview; scheduled posts also show their publish date.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find ""Preview" Banner on Unpublished Content" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
