Skip to content

Instantly share code, notes, and snippets.

@JoelLisenby
Last active February 19, 2022 20:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoelLisenby/ce8bd5d4b5a67cdb3242ef79d8ea5f0f to your computer and use it in GitHub Desktop.
Save JoelLisenby/ce8bd5d4b5a67cdb3242ef79d8ea5f0f to your computer and use it in GitHub Desktop.
Remove Flash of Unstyled Content in WordPress using SiteOrigin Page Builder and Mega Menu
<?php
function head_fouc_script() {
?>
<script>
document.body.style.setProperty("visibility", "hidden", "important");
</script>
<style>
#mega-menu-wrap-primary #mega-menu-primary { /* these IDs may differ depending on the name of your menu. */
visibility: inherit; /* mega menu sets visibility visible and shows if you don't set this */
}
</style>
<?php
}
add_action( 'genesis_before', 'head_fouc_script', 1 );
function footer_fouc_script() {
?>
<script>
// loadScript(scriptUrl): checks if a script has loaded yet
function loadScript(scriptUrl) {
const script = document.createElement('script');
script.src = scriptUrl;
document.body.appendChild(script);
return new Promise((res, rej) => {
script.onload = function() {
res();
}
script.onerror = function () {
rej();
}
});
}
var mega_menu_script = '<?php echo plugins_url() .'/megamenu/js/maxmegamenu.js'; ?>';
var siteorigin_script = '<?php echo plugins_url() .'/siteorigin-panels/js/styling.min.js' ?>';
loadScript(mega_menu_script).then(() => {
console.log('mega menu script loaded');
loadScript(siteorigin_script).then(() => {
console.log('siteorigin script loaded');
document.body.style.setProperty("visibility", "visible");
}).catch(() => {
console.log('error, script loading failed');
});
}).catch(() => {
console.log('error, script loading failed');
});
</script>
<noscript><style>body { visibility: visible !important; }</style></noscript>
<?php
}
add_action( 'admin_footer', 'footer_fouc_script', 100 );
add_action( 'wp_footer', 'footer_fouc_script', 100 );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment