Add a Preloader to WordPress Without a Plugin

A preloader is the loading screen visitors see while a page finishes loading: a full screen overlay with a spinner, logo, or short animation that fades away once the page is ready. Portfolios, agency sites, and stores use preloaders to make the first moments feel polished instead of blank or half rendered.
You do not need a plugin. A preloader is three small pieces: an HTML overlay near the top of the page, CSS that styles it and animates a spinner, and JavaScript that hides it when loading finishes. This guide builds all three, adds safety fallbacks so the overlay can never trap your page, and shows logo and homepage only variants.
What Is a Preloader and How Does It Work?
A preloader is a temporary layer covering the viewport while the browser loads images, fonts, scripts, and stylesheets. The overlay is simple HTML with a fixed position, so it sits above everything else. A CSS animation spins a circle or pulses a logo in the centre. When the window load event fires, a small script fades the overlay out and removes it, revealing the finished content.
That is the whole mechanism. WordPress only provides hooks for printing the overlay, styles, and script. Everything else is plain HTML, CSS, and JavaScript you control.
When Does a Preloader Help and When Does It Hurt?
Here is the honest part. A preloader does not speed up your site at all. The page loads at the same speed with or without one; a preloader only changes what visitors see while they wait. On a heavy homepage with a hero video and large images, a short preloader hides the ugly half loaded state. On a fast, simple page, it adds delay where none was needed and makes the site feel slower.
Used carelessly, a preloader hurts perceived performance and search visibility. If it stays until every asset loads, visitors stare at a spinner while usable content sits hidden, and search engines measure how quickly main content becomes visible. Keep it short and fade it quickly. If your site is genuinely slow, fix the cause first with our guide on why your WordPress site is slow, and check hosting with our web hosting guide.
- Good use: a visual homepage, portfolio, or store front page that loads large media and looks broken while assets arrive.
- Good use: a brief brand moment with your logo that lasts under two seconds on a normal connection.
- Bad use: a spinner on every page of a content site, forcing readers to wait before every article.
- Bad use: a long animation that plays to completion even after the page is ready. The page must win, always.
What Should You Prepare Before Adding the Code?
Use a child theme so updates cannot erase your work, and keep a backup before you start. Our guide to create a WordPress child theme covers the setup. Prefer not to edit theme files? A code snippets plugin handles the PHP parts, and the Customizer Additional CSS panel can hold the styles, as our guide to add custom CSS in WordPress explains.
Step 1: How Do You Add the Preloader HTML?
The overlay must appear before the main content paints. The wp_body_open hook fires immediately after the opening body tag, the earliest safe place to print visible HTML. Add this snippet to your child theme functions file or a code snippets tool.
function vtg_add_preloader() {
if ( is_admin() ) {
return;
}
?>
<div id="vtg-preloader" aria-hidden="true">
<div class="vtg-spinner"></div>
</div>
<noscript><style>#vtg-preloader { display: none; }</style></noscript>
<?php
}
add_action( 'wp_body_open', 'vtg_add_preloader' );
Two details matter. The is_admin check keeps the overlay out of the dashboard. The noscript block hides it when JavaScript is disabled, because without JavaScript nothing would remove it, and visitors must see your page rather than a permanent spinner. The wp_body_open hook documentation explains where this hook fires.
Step 2: How Do You Style the Overlay and Spinner?
Add this CSS to your child theme stylesheet or the Customizer Additional CSS panel. Fixed positioning covers the viewport, a high z-index sits above headers and popups, and flex layout keeps the spinner centred on every screen size.
#vtg-preloader {
position: fixed;
inset: 0;
background: #ffffff;
display: flex;
align-items: center;
justify-content: center;
z-index: 9999;
transition: opacity 0.4s ease;
}
#vtg-preloader.vtg-hidden {
opacity: 0;
pointer-events: none;
}
.vtg-spinner {
width: 48px;
height: 48px;
border: 5px solid #e5e7eb;
border-top-color: #2563eb;
border-radius: 50%;
animation: vtg-spin 0.8s linear infinite;
}
@keyframes vtg-spin {
to {
transform: rotate(360deg);
}
}
The spinner is a circle with one coloured edge, rotated by a keyframe animation. Change border-top-color to your brand colour and background to match your site. The vtg-hidden class is the exit state: opacity fades to zero, and pointer-events none ensures the fading overlay never blocks clicks. Without that rule, an invisible overlay can swallow every tap.
Step 3: How Do You Hide the Preloader with JavaScript?
The script listens for the window load event, adds the hidden class, waits for the fade, then removes the overlay entirely. Print it in the footer with this PHP wrapper.
function vtg_preloader_script() {
if ( is_admin() ) {
return;
}
?>
<script>
(function () {
function hidePreloader() {
var preloader = document.getElementById('vtg-preloader');
if (!preloader) {
return;
}
preloader.classList.add('vtg-hidden');
setTimeout(function () {
preloader.remove();
}, 450);
}
window.addEventListener('load', hidePreloader);
setTimeout(hidePreloader, 3000);
})();
</script>
<?php
}
add_action( 'wp_footer', 'vtg_preloader_script' );
The setTimeout call is the most important safety rule in this guide. It hides the preloader after three seconds no matter what, so a slow image or stuck third party script cannot delay your content. Never ship a preloader without a timeout fallback. A page trapped behind a stuck overlay is worse than no preloader at all.
What Safety Rules Must Every Preloader Follow?
- Timeout fallback: hide the overlay after three seconds at most, even if the load event never fires.
- Noscript fallback: hide the overlay with CSS inside a noscript block so visitors without JavaScript see the page immediately.
- Fade, then remove: fade opacity for polish, then remove the element from the page so it cannot intercept anything later.
- Pointer-events none: the hidden state must ignore clicks and taps from the first frame of the fade.
- Keep it short: the overlay should normally disappear in under two seconds. If your page regularly needs longer, fix the page speed instead of decorating the wait.
Do speed work first. Reduce image sizes, enable caching, and consider object caching for busy sites, as our guide to WordPress object cache explained for beginners describes. A preloader on a fast page is a garnish; on a slow page it is a curtain hiding a problem visitors still feel.
How Do You Use Your Logo Instead of a Spinner?
A logo preloader replaces the circle with your brand mark and a gentle pulse. Swap the spinner div in Step 1 for an image tag pointing at your logo, then style it with a pulse instead of a spin. Keep the logo small, ideally under 30 KB, because a heavy logo defeats the purpose.
<div id="vtg-preloader" aria-hidden="true">
<img class="vtg-preloader-logo" src="https://yoursite.com/wp-content/uploads/logo.png" alt="">
</div>
.vtg-preloader-logo {
width: 140px;
height: auto;
animation: vtg-pulse 1.2s ease-in-out infinite;
}
@keyframes vtg-pulse {
0% { opacity: 0.45; transform: scale(0.97); }
50% { opacity: 1; transform: scale(1); }
100% { opacity: 0.45; transform: scale(0.97); }
}
The empty alt attribute is deliberate, since the preloader is decorative and screen readers should skip it. Keep the same JavaScript and fallbacks; only the visual changes.
How Do You Show the Preloader Only on the Homepage?
Most sites should show a preloader on the homepage only, since readers moving between articles do not need a loading screen on every click. Add this condition to the Step 1 function, and the same condition to the Step 3 script function.
function vtg_add_preloader() {
if ( is_admin() || ! is_front_page() ) {
return;
}
// Overlay HTML from Step 1 goes here.
}
The is_front_page check covers a static homepage and a latest posts homepage. For a separate blog index, is_home is the matching condition. Pick the condition for the page visitors land on first, and leave other pages instant.
What If Your Theme Does Not Support wp_body_open?
The wp_body_open hook needs theme support. Classic themes must call it in header.php after the opening body tag, and well built themes have done so for years. Block themes and full site editing setups support the hook through the template system in current WordPress versions, so the snippet usually works there too. If the overlay never appears, your theme is not firing the hook.
Two alternatives work. First, print the overlay through wp_footer. Fixed positioning still covers the screen even though the HTML arrives late, though you may see a brief flash before it paints. Second, for block themes, add the overlay HTML through a Custom HTML block in the header template part in the Site Editor, put the CSS in global styles, and keep the script in the wp_footer wrapper. Keep both fallbacks either way. The WordPress.org theme handbook covers hook support in detail.
How Do You Test Your Preloader Properly?
- Throttle the network: open your browser developer tools, set the network throttle to Slow 3G, and reload. The spinner should appear, animate smoothly, and fade away when loading completes.
- Test the timeout: keep the throttle on and watch the clock. The overlay must disappear by three seconds even while assets are still loading.
- Disable JavaScript: turn scripts off in developer tools and reload. The page must appear instantly with no overlay. This confirms the noscript fallback works.
- Test on mobile: use a real phone or the device toolbar at a small width. The spinner should stay centred, the fade should feel quick, and taps should work the moment the overlay fades.
- Test inner pages: if you chose the homepage only variant, open an article and confirm no overlay or script loads there.
- Check clicks during the fade: click a link while the overlay is fading. The click must reach the page, which confirms pointer-events none is doing its job.
If any test fails, remove the preloader until you fix the cause. A broken preloader costs you every visitor who hits it; a missing one costs only a little decoration.
Frequently Asked Questions
Does a preloader make my WordPress site faster?
No. A preloader changes what visitors see while the page loads, but loading takes the same time. It can polish a heavy homepage, yet make a fast page feel slower. Fix real speed problems first.
Can a preloader hurt my SEO?
It can if it hides your main content for too long, because search engines measure how quickly the largest content becomes visible. Keep the overlay under two seconds on normal connections, always include the three second timeout fallback, and consider showing the preloader on the homepage only.
What happens if JavaScript fails or is disabled?
With the safety rules in this guide, nothing bad happens. The noscript CSS hides the overlay for visitors without JavaScript, and the three second timeout hides it when a script error or a stuck asset delays the load event. Never publish a preloader without both fallbacks.
Do I need a plugin to add a preloader to WordPress?
No. The three code pieces in this guide build a complete preloader with no plugin, and you keep full control over design, timing, and fallbacks.
Last updated: December 2026.



