Skip to content

Instantly share code, notes, and snippets.

@lkoudal
Forked from thetwopct/functions.php
Created October 27, 2020 22:24
Show Gist options
  • Select an option

  • Save lkoudal/5656a793c659a997a7d77cd7e34dcd98 to your computer and use it in GitHub Desktop.

Select an option

Save lkoudal/5656a793c659a997a7d77cd7e34dcd98 to your computer and use it in GitHub Desktop.
Preload any footer JS files in the header
/**
* Preload any footer JS files in the header
*
* originally from: https://macarthur.me/posts/preloading-javascript-in-wordpress
*/
add_action('wp_head', function () {
global $wp_scripts;
foreach($wp_scripts->queue as $handle) {
$script = $wp_scripts->registered[$handle];
//-- check if group exists
if (isset($script->extra['group'])) {
//-- Weird way to check if script is being enqueued in the footer.
if( $script->extra['group'] == 1) {
//-- If version is set, append to end of source.
$source = $script->src . ($script->ver ? "?ver={$script->ver}" : "");
//-- Spit out the tag.
echo "<link rel='preload' href='{$source}' as='script'/>\n";
}
}
}
}, 1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment