Yes! jQuery 1.5 was released and I’m sure some of you can’t wait to start using it in their WordPress themes and plugins. So, with a few filters magic, we can get jQuery 1.5 up and running. Add the following code to your theme’s function.php file:
add_filter( 'script_loader_src', 'my_script_loader_src', 10, 2 ); function my_script_loader_src( $src, $handle ) { if ( $handle == "jquery" ) return "http://code.jquery.com/jquery-1.5.min.js"; return $src; }
And that’s it! Now every call to wp_enqueue_script jquery will pass through this filter and load up 1.5 instead of the one shipped with your WordPress bundle. Feel free to change the URL of the jQuery script to different CDNs including Microsoft’s and Google’s.
I guess this post will be outdated upon the next WordPress release, but it still may be valuable, since the same trick is used to run jQuery and other libraries shipped with WordPress from different sources like Google or Amazon CloudFront.
[…] This post was mentioned on Twitter by Konstantin Kovshenin, rudevich and Michael Davis, Amor. Amor said: Snippet: Using jQuery 1.5 in WordPress http://bit.ly/hqtJv5 – via @kovshenin […]
Useful snippet but what about this:
[code lang="php"]
function my_init() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://code.jquery.com/jquery-1.5.min.js', false, '1.5');
wp_enqueue_script('jquery');
}
}
add_action('init', 'my_init');
[/code]
Why you use your method?
Johnny, good questions. I believe filters are faster than deregister/register calls. Also, your version enqueues the jQuery script, while my version says rewrite to 1.5 if and only if jQuery is asked for (by themes or plugins). But anyways, perhaps this needs some profiling and benchmarking. Thanks for your input!
Oh, that sounds logical. Thanks ;)
[…] Kovshenin […]
Looks like jQuery 1.5 breaks some areas in the WordPress admin, so this should only be applied for the frontend:
http://wpdevel.wordpress.com/2011/02/10/if-your-m…
Most of the new stuff breaks the old stuff ;) yeah, a call for is_admin() could be good.
Changing 1.5 to 1.5.1 makes it work even better ;-)
Right :) Cheers!
Thanks for the tip! I've got my jquery.mobile up and running now… =D