Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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