If you are using Field Dependency in your search form, you may sometimes notice a visual issue: fields inside the form can “jump” or reflow while the page is loading. This happens because dependencies and layout adjustments are calculated by JavaScript only after all scripts and styles are ready.
To avoid this, you can use a simple workaround that hides the search form until the page is fully loaded, and then reveals it smoothly.
Tip: If you don’t already have a child theme, start here:
Utilizing a child theme: modifying PHP, CSS, JS files while maintaining future update compatibility
The Problem It Solves
Users who enable Field Dependency may see fields shifting or resizing during the loading phase.
This can cause a poor user experience, especially if the search form is placed in a visible area like the homepage hero section.
The solution is to hide the form entirely until the page is ready, preventing any layout shifts from being visible to the visitor.
The Workaround
We will do this in two steps:
Hide the form by default using CSS inside your child theme.
Reveal the form only after the page is fully loaded by adding a small JavaScript snippet through your child theme’s
functions.php
.
Step 1: Add CSS to style.css
Open your child theme’s style.css
and add:
This ensures the form is hidden initially, no matter what other styles are applied.
Step 2: Add JavaScript via functions.php
Open your child theme’s functions.php
and add:
This script waits until the entire page (including images, fonts, and external resources) is loaded. At that point, it adds a page-loaded
class to <body>
. The CSS rule from Step 1 detects this class and reveals the form.
How It Works
When the page starts loading, the form is hidden by CSS (
display:none !important
).Once everything has finished loading, the JavaScript snippet adds a
.page-loaded
class to<body>
.The second CSS rule makes the search form visible again.
As a result, your visitors only see the fully initialized form, without any field jumping or layout glitches.
✅ That’s it! You’ve successfully hidden the form during load and improved the user experience for your search form with field dependencies.