Last active
January 11, 2018 11:17
-
-
Save shirokoweb/1239e61e8747631be9ee5e6799e1f950 to your computer and use it in GitHub Desktop.
async / defer wordpress scripts
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 js_async_attr($tag){ | |
# Do not add defer or async attribute to these scripts | |
$scripts_to_exclude = array('script1.js', 'script2.js', 'script3.js'); | |
foreach($scripts_to_exclude as $exclude_script){ | |
if(true == strpos($tag, $exclude_script ) ) | |
return $tag; | |
} | |
# Defer or async all remaining scripts not excluded above | |
return str_replace( ' src', ' defer="defer" src', $tag ); | |
} | |
add_filter( 'script_loader_tag', 'js_async_attr', 10 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment