Load newer version of jQuery until WordPress Core has that version

jQuery version 1.9 was released today, and I wondered how one could use that version on my site until it was released properly with WordPress. I found out that the $wp_scripts variable holds the script version of registered scripts. Thus, I was able to concoct the following function for the wp_enqueue_scripts hook. Change the prefix as you see fit. Also change the version as you see fit as well. This is fairly future proof as versions will never go down.

[code type=”php”]
add_action(‘wp_enqueue_scripts’, ‘prefix_load_scripts’);
function prefix_load_scripts() {
if(is_admin()) return;

global $wp_scripts;

if ( $wp_scripts->registered[‘jquery’]->ver != ‘1.9.0’ ) {
wp_deregister_script( ‘jquery’ );
wp_enqueue_script(‘jquery’, ‘http://code.jquery.com/jquery-1.9.0.min.js’);
} else {
wp_enqueue_script(‘jquery’);
}
}
[/code]

I won’t deny that I’m a believer in using the version of jQuery that WordPress comes packaged with, but if you are anxious to try out the new stuff and not worry about reverting back manually, this would be a way to go.

Is your current hosting dragging you down and you want to find a better place to host your WordPress site? You should head over to WPEngine and sign up. Use my affiliate link to tell them I sent you.

4 thoughts on “Load newer version of jQuery until WordPress Core has that version

  1. Hi,
    first thanks fot the code. but i get error:
    PHP Parse error: syntax error, unexpected ‘&’
    in this line:
    if ( $wp_scripts->registered[‘jquery’]->ver != ‘1.9.0’ )
    i think it becuse ->

    what i do wrong?

    thanks

    1. Not your fault, mostly mine because I haven’t fixed encoding stuff. You need to change the > into > So you’d want:

      if ( $wp_scripts->registered[‘jquery’]->ver != ’1.9.0′ )

Leave a Reply

Your email address will not be published. Required fields are marked *