Make your WordPress plugins use a different version of a bundled JavaScript library
Short URL for this post: http://plp.me/a40DuvI've recently coded my first WordPress plugin, for the fun of it. I've been using WordPress for some time, and I know PHP quite well, so I figured it'd be fun. WordPress has a weird/interesting way of being extendable. I won't go into too much technical details about that, but suffices to say that it's easy yet complicated to perform tasks you want to. Yeah, that weird.
All that being said, I upgraded my WordPress from 2.7 to 2.7.1 in the middle of coding some new features for the latest release of WP-prettyphoto, 1.1. That made me realize that updating WordPress' core also updates the installed JavaScript modules, namely jQuery (among many others). When I first coded WP-prettyphoto, when things weren't working I just replaced the jQuery I had with the latest official stable release and things worked fine. I didn't give it much thought. So when I updated WordPress, all hell broke loose and WP-prettyphoto was metaphorically crying in agony because jQuery wasn't man enough for it. I then tried performing the same manual updating procedure, and without really surprising anyone -- I was alone anyway -- WP-prettyphoto was back prettyfying my stuff like a starving fat kid eats a cheesecake.
Morale of the story, I needed to code a jQuery version check in my WP-prettyphoto code, and if jQuery's version wasn't matching what I wanted, I had to replace the script with one that would make WP-prettyPhoto work the way it's supposed to. The goal's clear, the execution, however, was somewhat more obscure. Obviously, not being a WordPress plugin genius, I googled for solutions or anything that could help me achieve what I wanted. I didn't find much on how to replace a bundled script loaded by default. I found what I was looking for in a competing (and I use that term very, very loosely!) plugin for a jQuery Lightbox clone cleverly called jQuery Lightbox by Pedro Lamas. It's basically de-registering the default WordPress jQuery plugin and replacing it with one bundled with the plugin. Using native functions. Nice. Here's the code:
// jQuery - removing to make sure we're using 1.3.1
//Deregister bundled jQuery (1.2.6 as of WP 2.7.1)
wp_deregister_script('jquery');
//Registering our up to date jQuery
//$wppp_url is WP_CONTENT_URL.'/plugins/'.plugin_basename(dirname(__FILE__));
wp_register_script('jquery', ("$wppp_url/js/jquery-1.3.1.min.js"), false, '1.3.1');
//Giving WordPress our new jQuery script to enqueue (display)
wp_enqueue_script('jquery');
So there you have it. That's how to replace the version of a bundled WordPress JavaScript library.
