How can I remove version query var from jquery include function in WordPress

There are several similar questions here but none that seem to address my specific case, at least not that I found. Here is the code that I use to include latest jquery version in my WP theme, and also to make sure it appears in the footer.

function current_jquery($version) {
    global $wp_scripts;
    if ( ( version_compare($version, $wp_scripts -> registered[jquery] -> ver) == 1 ) && !is_admin() ) {
        wp_deregister_script('jquery');
        wp_register_script('jquery',
            'http://ajax.googleapis.com/ajax/libs/jquery/'.$version.'/jquery.min.js',
            false, $version, true);
    }
}
add_action('wp_head', current_jquery('1.5.1'));

I then use wp_enqueue_script("jquery"); in header.php and it works, except that I get a jquery include that ends with “jquery.min.js?ver=1.5.1”, and I want to get rid of the query part.

Read More

I checked the wp_register_script function and from what I read to get rid of the query I need to just replace the 2nd from last variable with blank string ”. However, when I do that I get the “default” wordpress version instead, i.e. “jquery.min.js?ver=3.0.1” (or whatever it may happen to be at the time).

Because I don’t 100% understand everything that’s going on here (between current_jquery, add_action, and wp_enqueue_script) I’m not even sure where to start (is $version getting assigned WP version value somehow?) All I know is that wp_register_script doesn’t seem to respond to version input the way it’s documented. What am I missing here?

I should add that I’d like to avoid using string operations (split, reg_replace, what have you) on the output “src” string to do this, because there must be a better way.

Related posts

Leave a Reply

1 comment

  1. The comments above wp_register_script suggest passing NULL to disable the version query-string:

     * @param string|bool $ver (optional) Script version (used for cache busting), set to NULL to disable
    

    and it looks like it’s being explicitly compared with null in WP_Scripts:

     if ( null === $this->registered[$handle]->ver ) // Line 93 in 3.0
    

    …so I’d pass in null rather than an empty string and see how you get on.