2

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 );
1
  • What do you think in register a new js that calls Jquery? – Tiago Fabre Aug 23 '16 at 12:22
4

Use script_loader_tag to modify the HTML before printing.

apply_filters( 'script_loader_tag', string $tag, string $handle, string $src )


add_action( 'init', 'replace_jquery_src' );

/**
 * Modify loaded scripts
 */
function replace_jquery_src() {
    if ( ! is_admin() ) {

        // Remove the default jQuery
        wp_deregister_script( 'jquery' );

        // Register our own under 'jquery' and enqueue it
        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_filter( 'script_loader_tag', 'add_async_to_jquery', 10, 3 );

/**
 * Filter script tag output
 *
 * @param string $tag    HTML output
 * @param string $handle registered name
 * @param string $src    path to JS file
 *
 * @return string
 */
function add_async_to_jquery( $tag, $handle, $src ) {

    // Check for our registered handle and add async
    if ( 'jquery' === $handle ) {
        return str_replace( ' src=', ' async src=', $tag );
    }

    // Allow all other tags to pass
    return $tag;
}
6
  • Thanks! But i am not a php expert, can you please modify my code and give me new code, So i can add directly in function.php file !! – Yashveer Arya Aug 23 '16 at 18:31
  • I am using this code... But not working add_filter( 'script_loader_tag', function ( $tag, $handle ) { if ( 'jquery/1.11.3/jquery.min.js' !== $handle ) return $tag; return str_replace( ' src', ' defer="defer" src', $tag ); }, 10, 2 ); – Yashveer Arya Aug 23 '16 at 19:02
  • That looks about right. Just double check what is showing in $handle. I imagine the $handle would be 'jquery' like you registered above – jgraup Aug 23 '16 at 21:43
  • tried this and several other things, but not working. Also this question is marked this as Duplicate even other question is much more different than this !! – Yashveer Arya Aug 24 '16 at 5:13
  • The above code is a working example. $handle is not part of the $src like you have it, it's the registered name – jgraup Aug 24 '16 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.