Skip to content

Instantly share code, notes, and snippets.

@jevgen
Forked from yankiara/wp-defer-scripts.js
Created July 10, 2021 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jevgen/4b39269c002ccc2c5c44311a8a28a196 to your computer and use it in GitHub Desktop.
Save jevgen/4b39269c002ccc2c5c44311a8a28a196 to your computer and use it in GitHub Desktop.
Defer enqueued JS scripts in Wordpress
add_filter( 'script_loader_tag', 'yankiara_defer_scripts', 10, 3 );
function yankiara_defer_scripts( $tag, $handle, $src ) {
$defer = array( 'script-handle-1', 'script-handle-2', 'script-handle-3' );
if ( in_array( $handle, $defer ) )
return '<script src="' . $src . '" defer="defer" type="text/javascript"></script>' . "\n";
return $tag;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment