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.
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');
}
}
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.
And when 2.0 ships? …
Obviously I’d have to update this. 🙂
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
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′ )