---
url: https://docs.artemsemkin.com/velum/wp/developers/adding-a-preloader.md
description: >-
  Cover the page with a preloader of your own while it loads, hold Velum's
  entrance animations until it leaves, show it once per visit, or wait for a
  video or an animation of yours instead.
---

# Adding a preloader

A preloader covers the page while it loads and reveals it when it's ready. Velum has none of its own, and it starts the page's entrance animations as soon as the page has loaded: behind a cover of yours they would play unseen. The code below holds Velum's start until your preloader leaves, so the page enters as the cover fades out.

## Printing the preloader

The preloader goes into `wp_body_open`, outside the page content that page transitions replace, so it shows on the first page the visitor opens and never comes back on a page change. A small script in the `<head>` marks the page while the preloader is up. In the child theme's `functions.php`:

```php
add_action( 'wp_head', 'velum_child_preloader_flag', 1 );
function velum_child_preloader_flag() {
	wp_print_inline_script_tag(
		"if (document.readyState === 'loading') { document.documentElement.classList.add('is-preloading') }"
	);
}

add_action( 'wp_body_open', 'velum_child_print_preloader' );
function velum_child_print_preloader() {
	?>
	<div class="site-preloader">
		<span class="site-preloader__label"><?php bloginfo( 'name' ); ?></span>
	</div>
	<?php
}
```

In the child theme's `style.css`:

```css
.site-preloader {
  display: none;
}

.is-preloading {
  overflow: hidden;
}

.is-preloading .site-preloader {
  display: flex;
  align-items: center;
  justify-content: center;
  position: fixed;
  inset: 0;
  z-index: 10000;
  background-color: #000;
  color: #fff;
}
```

## Holding Velum's start until it leaves

Load this script as described in [Loading a script that works with Velum](./extending-velum-with-code#loading-a-script-that-works-with-velum), so it runs before Velum starts:

```js
const preloader = document.querySelector('.site-preloader')
const root = document.documentElement

if (preloader && root.classList.contains('is-preloading')) {
  const engine = window.__artsEngine__
  let done = false

  engine.hooks.addFilter('arts_runtime/should_auto_bootstrap', () => false)

  const finish = () => {
    if (done) {
      return
    }
    done = true
    engine.initialLoad()
    preloader
      .animate([{ opacity: 1 }, { opacity: 0 }], { duration: 600 })
      .finished.then(() => {
        preloader.remove()
        root.classList.remove('is-preloading')
      })
  }

  window.addEventListener('load', finish)
}
```

The `arts_runtime/should_auto_bootstrap` filter stops Velum from starting by itself, and `engine.initialLoad()` starts it: the widgets set up and their entrance plays while the preloader fades. Here that happens on the window's `load` event, once the page's images and files have loaded.

## Showing it once per visit

To show the preloader only on the first page of a visit, remember it in `sessionStorage`. Check the flag in the `<head>` script:

```php
"if (document.readyState === 'loading' && !sessionStorage.getItem('preloader-seen')) { document.documentElement.classList.add('is-preloading') }"
```

And set it in `finish()`, next to `engine.initialLoad()`:

```js
sessionStorage.setItem('preloader-seen', '1')
```

A new tab starts a new visit. To show it only once ever, use `localStorage` instead. To show it on the homepage only, return early from both PHP functions with `if ( ! is_front_page() ) { return; }`.

## Waiting for a video or an animation of yours

`finish()` can run on any signal, not only on `load`: when a logo animation of yours ends, or when an intro video ends or the visitor skips it. For a video, put a muted `<video>` with `playsinline` and a skip button into the preloader, and replace the `load` listener with:

```js
const video = preloader.querySelector('video')

video.addEventListener('ended', finish)
preloader.querySelector('.site-preloader__skip').addEventListener('click', finish)
video.play().catch(finish)
```

Browsers play a video by themselves only when it's muted, and `playsinline` keeps an iPhone from opening it full screen. `video.play()` fails when the browser still won't play it, for example on an iPhone in Low Power Mode: the preloader then leaves at once.
