Skip to content

Instantly share code, notes, and snippets.

@metaylimpo
Last active June 8, 2020 23:31
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 metaylimpo/dc6fb15365e386d00c7cb0531f865d12 to your computer and use it in GitHub Desktop.
Save metaylimpo/dc6fb15365e386d00c7cb0531f865d12 to your computer and use it in GitHub Desktop.
Example how to defer js in Wordpress with function.php file
<?php
if (!is_admin()) {
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
if ( strpos( $tag, "jquery.js" ) ||
strpos( $tag, "leaflet.js") /*||
strpos( $tag, "add any script you want" )*/ ) {
return $tag;
}
return str_replace( ' src', ' defer="defer" src', $tag );
}, 10, 2 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment