@tijmensmit our fix looks like the following:
add_filter('script_loader_tag', 'gmap_scripts_defer', 10, 2);
function gmap_scripts_defer($tag, $handle)
{
if ($handle === 'wpsl-js') {
return str_replace(' src=', ' onload="document.dispatchEvent(new CustomEvent(
wpslJsLoaded
))" src=', $tag);
}
if ($handle === 'wpsl-gmap') {
preg_match('/<script.*?src=["\']+(.*?)["\']+/', $tag, $matches);
if (!isset($matches[1]) || empty($matches[1])) {
return $tag;
}
$tag = '<script>function initMapsScript(){const src = ' . str_replace('&', '&', $matches[1]) . '
;const script = document.createElement(script
);script.src = decodeURI(src);document.body.append(script);}
if(typeof wpsl === "undefined"){document.addEventListener(wpslJsLoaded
, initMapsScript);} else {initMapsScript();}</script>';
}
return $tag;
}
-
This reply was modified 1 year, 7 months ago by markd33.