While WordPress ships with jQuery, it’s often a few updates behind the latest version. Since jQuery 1.4 is just out, I wanted to use it with K2. That in and of itself is fairly easy, and a simply matter of deregistering the ‘jquery’ script and registering a new one. While looking for a proper solution I came across this rather crude way of going about it quite a lot, and it’s a horribly way of going about this, and will (and has probably) undoubtedly result in old plugins and themes blindly overwriting newer version of jQuery with their own, once new hotness, now old and busted version of jQuery.
Ugh.
This PHP code snippet checks to see if the passed version is later than the one currently registered, and makes sure we’re not in the admin (just to be sure).
If our version is indeed newer than the one currently registered, we go ahead and grab the idol… eh, swap jQuery’s.
/**
* Register a later version of jQuery if it’s later than the one currently in WordPress
*
* @param {String} our_version The version of jQuery we want to upgrade to if needed.
*/
function upgrade_jquery( our_version ) {
// We want to use the latest version of jQuery, but it may break something in
// the admin, so we only load it on the actual site.
global $wp_scripts;
if ( ( version_compare(our_version, $wp_scripts -> registered[jquery] -> ver) == 1 ) && !is_admin() ) :
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’,
get_bloginfo(‘template_directory’) . ‘/js/jquery.js’,
false, our_version);
endif;
}
add_action( ‘wp_head’, upgrade_jquery( ’1.4.1’ ) );
It sure would be neat if this was built straight into WordPress’ wp_register_script.
Maybe in the next version of WordPress ;)
Thanks for the snippet. What would be WordPress without you? :)
I use this:
if ( !is_admin() ) :
wp_deregister_script( ‘jquery’ );
wp_register_script( ‘jquery’, ( ‘http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js’ ), false, ’1.4.1’, false );
//wp_enqueue_script( ‘jquery’ );
endif;
Which force unloads without looking to see if what’s being force unloaded is newer.
Awesome, Michael!
I don’t suppose there’s a way to keep this fully dynamic and always grab the latest version of jQuery? I have themes in mind. And pushing out updates to simply update the version of jQuery that’s bundled isn’t exactly efficient.
It’s also not necessary most of the time, but for the sake of argument, you could simply load it off your own server or something like that, though that’s a bit ugly. Beyond that I supposed you could set up some sort of system to check for updates on your (or jQuery’s) server and auto-download them when they become available. Though I think that may be a bit much.
I’ll probably get scolded for this but … my comment isn’t about jQuery, but iPad (comments closed on past posts) I’ll try to be brief:
Consider for a moment the automobile industry, which steadily cycles their ad campaigns from small to large and back again: Sports cars (porsche) » Vans (VW minibus) » Sedans » Huge SUVs » Tiny hybrids (Smart Car) » Hybrid SUVs = one can easily argue that these fads had little to do with supply/demand and LOTS to do with marketing strategy. When we have big, sell us “smaller and more efficient.” When we have small, sell us “bigger and better.”
Now switch to computers which constantly become more compact and efficient and yet: CD-Rs, flash memory cards, DVD-Rs, external hard drives, 16GB thumb USB drives, external terrabyte drives, iPod Nano, iMac flat LCD screen, iPod 80GB, Apple “MyBook?” iPhone 16GB, iPad … oh do you play games and watch movies on your iPad? how about this one: TV » portable TV » projection TVs » portable color TV » NES » gameboy » Super NES » backlit color gameboy » Xbox » portable DVD players » Laptops with superior video graphics » big » small » bigger » smaller … it will go on forever
We were all pretty happy with iPhones until we heard rumors about a larger “iTablet” WHY??? Did anyone ever really believe the stories about “trying to save the newspaper and magazine industry” ???
Like Michael said recently, it’s not a fucking laptop. So why do we feel the need? Because it’s a “bigger and better” version of iPhone? I’ll bet cold hard cash that a few years from now they unleash a smaller, thinner, faster version of iPhone. Perhaps the Dick Tracy wrist watch. Remember when those were cool? Yeah, it was right before Texas Instruments unleashed their TI-89 graphing calculators. Lord knows why I felt the need to buy that behemoth device … oh I remember, so I could play games during Calculus (and it wasn’t even in color).