2

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.

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.

3
  • Just out of curiosity -- why do you want to remove the query part? Commented Mar 10, 2011 at 9:26
  • 1
    I'm going on the assumption that more sites will be using a "manual" include of jQuery and so they'll simply reference "ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js" without the query string. Which means the file is going to be already cached in the users browser and they won't need to download it again when they visit my site. Query string messes that up. Commented Mar 10, 2011 at 13:41
  • Ah, yes, makes perfect sense. Commented Mar 10, 2011 at 13:43

1 Answer 1

6

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Of course! I feel so stupid. That was exactly it. I'm going to trust comments in code more than the documentation from now on :) Thank you
@ilia About the only thing you can really trust is the code itself :) You're welcome!
very nice! I have been looking for this from ages.. :) Never thought of digging in the WP core code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.