Last active
August 9, 2018 14:05
-
-
Save evemilano/a20419b800105c090c1e81d6ae701789 to your computer and use it in GitHub Desktop.
Add Defer & Async Attributes to many WordPress Script
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
function add_defer_attribute($tag, $handle) { | |
// add script handles to the array below | |
$scripts_to_defer = array('jquery-migrate', 'hoverIntent', 'superfish', 'superfish-args', 'skip-links', 'wp-embed'); | |
foreach($scripts_to_defer as $defer_script) { | |
if ($defer_script === $handle) { | |
return str_replace(' src', ' defer="defer" src', $tag); | |
} | |
} | |
return $tag; | |
} | |
add_filter('script_loader_tag', 'add_defer_attribute', 10, 2); | |
//* If you want to use defer just replace async="async" with defer="defer". | |
//* You will need to change the handle which will be the first parameter of the enqueue method. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full article: : https://www.evemilano.com/2017/10/aggiungere-defer-async/