Chapter 2. Working with jQuery in WordPress

Now that we understand the basics of jQuery and WordPress and have a little background on how they'll interact with each other, we're now ready to take a look at using jQuery to dynamically enhance a WordPress installation. We'll start with getting jQuery included in WordPress and end up with our first cool project: Expanding and collapsing content. This is only the beginning of the jQuery possibilities in store for your WordPress site! Again, we'll be using WordPress 3.0 in this title and the new default Twenty Ten theme with jQuery 1.4.2, but rest assured that if your site or project is still using WordPress 2.9, these jQuery techniques will work just fine.

In this chapter, we'll cover the following topics:

jQuery can be included into WordPress in three different ways as follows:

We covered the basics of the first method in Chapter 1, Getting Started: WordPress and jQuery. WordPress is so flexible that any user with the right admin level can come along and update, enhance the theme, or install additional plugins which may also use a version of jQuery or other JavaScript libraries. Therefore, including jQuery or any JavaScripts directly into the theme with hardcoded script tags is not recommended as it could cause conflicts with other scripts and libraries included into the WordPress site through theme customizations or plugins added to the WordPress installation. In this chapter, let's take a look at using the two remaining methods, registering jQuery through WordPress' Script API and using Google's CDN.

As of WordPress 2.7, jQuery and several other JavaScript libraries and plugins have been bundled and are available through WordPress' Script API through a handy function called wp_enqueue_script. Actually, WordPress has had jQuery and quite a few other JavaScript libraries (including Script.aculo.us with Prototype and many more) bundled into the wp-includes directory for some time, but until version 2.7, these includes were not so easily accessible.

Personally, I am a little torn about registering and referencing the copy that comes with WordPress. I've discovered that loading the library from Google Code's Code Distribution Network (CDN) is sometimes a better way to go. The CDN saves on bandwidth, allowing your site to do some parallel processing while downloading other scripts and collateral. Plus, it's easy to always get the most current version of jQuery. jQuery's library loads very quickly from Google's CDN and, as a bonus, the library will already be cached if your site's user has previously visited another site that delivers jQuery from Google Code's CDN.

To include jQuery from Google Code's CDN, we'll be sure to deregister jQuery then register through Google's CDN. This is the beauty of registering and using the wp_enqueue_script function: if any other plugin or script requires jQuery, and doesn't have any conflicts with the version loading up from Google, that script will use the already loaded Google CDN library. If a script depends on a specific version of jQuery, say 1.3.2 or 1.2.6, and the CDN is loading up version 1.4.2, then that script will go ahead and load the version of jQuery it requires. Because (as we'll learn) every script loaded through the Script API stays in noConflict mode, it's OK to have the two library versions loaded as long as they're registered and required.

Google offers a great versioning system that allows you to be as precise as you want, or just pull the latest stable version. Consider the previous code example (note the highlighted number, 1.4, in the previous code example).