Created
May 15, 2017 11:20
-
-
Save obiPlabon/b8fa708d2041010a9f2aa51985ae3642 to your computer and use it in GitHub Desktop.
Modify script tag in WordPress to load scripts in async mood or defer mood
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
<?php | |
/** | |
* Add async boolean attribute to script tag | |
* @param string $tag Default script tag | |
* @return string Modified script tag | |
*/ | |
function omm_add_async_loading( $tag ) { | |
return str_replace( '<script', '<script async', $tag ); | |
} | |
add_filter( 'script_loader_tag', 'omm_add_async_loading' ); |
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
<?php | |
/** | |
* Add defer boolean attribute to script tag | |
* @param string $tag Default script tag | |
* @return string Modified script tag | |
*/ | |
function omm_add_defer_loading( $tag ) { | |
return str_replace( '<script', '<script defer', $tag ); | |
} | |
add_filter( 'script_loader_tag', 'omm_add_defer_loading' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Worked perfectly. Thank you.