I am using this code to disable wordpress default jquery and load from Google library to speed up website and reduce latency for this Big File.
But Here i want to make it defer or async loading, So How we can do this ?
Here is code which i am using in my function.php file:-
//Making jQuery to load from Google Library
function replace_jquery() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js', false, '1.11.3');
wp_enqueue_script('jquery');
}
}
add_action('init', 'replace_jquery');
I have tried this in my functions.php
, but it does not work:
add_filter( 'script_loader_tag', function ( $tag, $handle ) {
if ( 'jquery/1.11.3/jquery.min.js' !== $handle )
return $tag;
return str_replace( ' src', ' async="async" src', $tag );
}, 10, 2 );