Preview Mode

Switch off cookie banners, animations, and other live-only code in the preview from PHP, CSS, and JavaScript, and see where links inside the preview lead.

The preview renders your site's own templates, CSS, and JavaScript. Detect preview mode to hide elements or switch off features there – the cookie banner, for example, or page transitions. Links inside the preview follow their own rules, listed under Link Navigation.

Backend

In the preview, $page->previewMode() is true:

if ($page->previewMode()->isTrue()) {
  // Page is in preview mode
}

Outside it, the content field is empty, so isFalse() holds. Wrap what the preview should not show, such as the cookie banner, in a check:

site/snippets/footer.php
<?php if ($page->previewMode()->isFalse()): ?>
  <?php snippet('cookie-banner') ?>
<?php endif ?>

The unsaved changes are on the edited model as Kirby hands it to the template – $page, or $site in a site blueprint. The same page fetched another way, such as page('about'), returns its saved content.

Frontend

In the preview, the <html> element carries a data-preview-mode attribute:

/* Hide the cookie banner in preview mode */
[data-preview-mode] .cookie-banner {
  display: none;
}

Clicking a link inside the preview opens the matching Panel view instead of following the link. Five kinds of links take another route:

  • Anchors with a fragment stay in the preview. A fragment-only link like #team scrolls to its target. Any other fragment link to a page of the site, like /about#team, loads that page as saved, without unsaved changes.
  • Anchors marked with data-preview-ignore behave like ordinary links inside the preview – the preview then shows the linked page as saved.
  • Links outside the site – to another origin, or outside the folder of a site in a subfolder – open in a new browser tab, mailto: and tel: links included.
  • Links below /assets/ or /media/ do nothing – neither the Panel nor the preview moves.
  • javascript: and data: links do nothing either.
site/snippets/header.php
<a href="<?= $page->url() ?>" data-preview-ignore>Stay in the preview</a>