Last active
May 21, 2017 05:40
-
-
Save georgestephanis/2a84bc55ad23f4dec2cf2464109add59 to your computer and use it in GitHub Desktop.
This file contains 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 | |
/** | |
* The only point of this is to add the `defer` attribute to | |
* a script (in this case, jQuery) to test with. | |
*/ | |
add_action( 'wp_print_scripts', function() { | |
wp_script_add_data( 'jquery-core', 'defer', true ); | |
} ); | |
/** | |
* This is the meat and potatoes -- checks for defer attribute and adds it to the tag. | |
*/ | |
add_filter( 'script_loader_tag', function( $tag, $handle, $src ) { | |
$scripts = wp_scripts(); | |
$obj = $scripts->registered[ $handle ]; | |
if ( ! empty( $obj->extra['defer'] ) ) { | |
$tag = str_replace( " src='$src'>", " src='$src' defer>", $tag ); | |
} | |
return $tag; | |
}, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment