Friday, February 13th, 2009 | Author: pluc | Views: 247

I’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.


Category: Coding
You can leave a response, or trackback from your own site.

8 Comments

  1. Very useful tip, I’ve needed to do this a couple of times. thanks!

  2. [...] versione jQuery si trova nella cartella del plugin. Articolo originariamente scritto da pluc: Make your Wordpress plugins use a different version of a bundled JavaScript library. Adattato e tradotto con il consenso dell’ autore. [...]

  3. Hi, thanks for this useful tip, but i want to ask a question to you.
    Is there any possibility to get a list of scripts to re-register? I mean, let the users write some script names into a textarea then plugin deregisters and registers these scripts.
    And, is that way usable for all versions of WP or should we follow different ways for different versions?
    Thanks right now..

  4. @Alper You can find a list of WordPress default scripts on the Codex (http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress) and as far as I know, there’s no plugin that has been released to give users the power to replace scripts, but it’d be a pretty good idea, I might actually create one. And as far as WordPress says, this should be compatible with all recent WordPress versions (I’m assuming 2.6+)

  5. Actually, i am creating one but not the aim of plugin is just re-resigter scripts, i want to add extra power to my plugin by allowing let the users can develop their functions and use new versions of scripts instead of old ones.. If you plan (or develop) to do that and if you inform me, i’ll be glad.. Regards.

  6. @Alper Already working on it

  7. [...] would prefer to use a different or newer version of the same library. You can read the article on Pier-Luc’s blog. Categories: Technical Writing Tags: howto, javascript, wordpress Comments (0) Trackbacks [...]

  8. Hi, I came across this blog post while searching for help with JavaScript. I have recently switched browsers from Google Chrome to Mozilla Firefox 3.1. Now I seem to have a problem with loading JavaScript. Everytime I go on a site that requires Javascript, my browser does not load and I get a “runtime error javascript.JSException: Unknown name”. I can’t seem to find out how to fix it. Any aid is very appreciated! Thanks

Leave a Reply