How to control what jQuery version to include, with wp_enqueue_script

I’m using a script that is based on jQuery, so I’m not enqueue-ing jQuery directly, but via passing it in the array of dependent scripts:

 wp_enqueue_script("jquery-plugin-name",'http://hostname.il/blogname/wp-content/themes/twentyten/js/jquery-plugin-name.js', array('jquery'), '1');

This is loading jQuery v1.4.4, and it weighs 76kb, which seems very heavy, although the file looks compressed.

Read More

I have 2 questions:

  1. Is there I way in which I can make it load the latest version of jQuery?
  2. How can I make sure that the loaded file if minified?

Related posts

Leave a Reply

3 comments

  1. That’s the size of the minified jQuery nowadays 🙂

    You can load the latest from Google:

    wp_deregister_script('jquery');
    wp_register_script('jquery', ("http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"), false, '1.8.2');
    

    Please keep in mind that this can cause issues moving forward, as you are forcing WordPress to load a certain version of jQuery instead of the version bundled with it. There are plugins out there that may be better suited for your needs, like WP JQuery Plus, which make sure to load the same version number as WordPress does, but with the benefit of loading it from Google.

  2. I have developed a plugin for this specific problem. This plugin doesn’t mess with WordPress jQuery as it is only loaded in the front-end. See: jQuery Manager for WordPress

    Almost everybody uses the incorrect handle

    WordPress actually uses the jquery-core handle, not jquery:

    The correct way to do it

    In my example below I use the official jQuery CDN at https://code.jquery.com I also use script_loader_tag so that I could add some CDN attributes.
    You could use the following code:

    // Front-end not excuted in the wp admin and the wp customizer (for compatibility reasons)
    // See: https://core.trac.wordpress.org/ticket/45130 and https://core.trac.wordpress.org/ticket/37110
    function wp_jquery_manager_plugin_front_end_scripts() {
        $wp_admin = is_admin();
        $wp_customizer = is_customize_preview();
    
        // jQuery
        if ( $wp_admin || $wp_customizer ) {
            // echo 'We are in the WP Admin or in the WP Customizer';
            return;
        }
        else {
            // Deregister WP core jQuery, see https://github.com/Remzi1993/wp-jquery-manager/issues/2 and https://github.com/WordPress/WordPress/blob/91da29d9afaa664eb84e1261ebb916b18a362aa9/wp-includes/script-loader.php#L226
            wp_deregister_script( 'jquery' ); // the jquery handle is just an alias to load jquery-core with jquery-migrate
            // Deregister WP jQuery
            wp_deregister_script( 'jquery-core' );
            // Deregister WP jQuery Migrate
            wp_deregister_script( 'jquery-migrate' );
    
            // Register jQuery in the head
            wp_register_script( 'jquery-core', 'https://code.jquery.com/jquery-3.3.1.min.js', array(), null, false );
    
            /**
             * Register jquery using jquery-core as a dependency, so other scripts could use the jquery handle
             * see https://wordpress.stackexchange.com/questions/283828/wp-register-script-multiple-identifiers
             * We first register the script and afther that we enqueue it, see why:
             * https://wordpress.stackexchange.com/questions/82490/when-should-i-use-wp-register-script-with-wp-enqueue-script-vs-just-wp-enque
             * https://stackoverflow.com/questions/39653993/what-is-diffrence-between-wp-enqueue-script-and-wp-register-script
             */
            wp_register_script( 'jquery', false, array( 'jquery-core' ), null, false );
            wp_enqueue_script( 'jquery' );
        }
    }
    add_action( 'wp_enqueue_scripts', 'wp_jquery_manager_plugin_front_end_scripts' );
    
    
    function add_jquery_attributes( $tag, $handle ) {
        if ( 'jquery-core' === $handle ) {
            return str_replace( "type='text/javascript'", "type='text/javascript' integrity='sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=' crossorigin='anonymous'", $tag );
        }
        return $tag;
    }
    add_filter( 'script_loader_tag', 'add_jquery_attributes', 10, 2 );
    
  3. Do not modify the version of jQuery enqueued by WordPress.

    Just don’t do it. Core depends on a specific version. Themes and Plugins depend on a certain version. That certain version is the version bundled with a given version of WordPress.

    If you need to perform script minification, compression, or concatenation, you can certainly do that (you can roll your own, or use a Plugin, such as W3 Total Cache, to do it for you.)

    But as you can see: if you’re got a 46kB file, you’re already dealing with some combination of minification and compression.