Created
November 7, 2018 12:14
-
-
Save zainaali/4d9415cfbe3c71c8e7e012bd24aaa3bd to your computer and use it in GitHub Desktop.
Automate JavaScript Preloading in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('wp_head', function () { | |
global $wp_scripts; | |
foreach($wp_scripts->queue as $handle) { | |
$script = $wp_scripts->registered[$handle]; | |
//-- 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