Using Java Script Libraries

WordPress includes a number of JavaScript libraries because it uses those libraries in the administration screens. They're available for you to use in your themes and plugins as well. The libraries include jQuery, Prototype, Scriptaculous, SWFUpload, and Thickbox. See codex.wordpress.org/Function_Reference/wp_enqueue_script for a complete list of the scripts available, along with their handles.

Listing 7-20 shows how to add the built-in jQuery and UI core libraries to your theme.

Listing 7-20. Including jQuery

wp_enqueue_script('jquery'); wp_enqueue_script('jquery-ui-core');

Using jQuery in WordPress is a bit tricky. Most jQuery scripts rely on a dollar sign function. For example, $("div.main").addClass("wide"); would add the wide class to a div that already had the main class. However, several other libraries, including Prototype, use this same convention. Because WordPress also uses Prototype, the jQuery library is loaded in "no conflict" mode (docs.jquery.com/Using_jOuery_with_Other_Libraries).

You have two options. You can use 'jQuery' in place of the dollar sign function ('$') throughout your script, or you can wrap your script in an extra function. Both methods are shown in Listing 7-21.

Listing 7-21. Using jQuery in WordPress jOuery("div.main").addClass("wide");

jOuery(document).ready(function($) {

// $() will work inside this function; otherwise use jOuery() $("div.main").addClass("wide");

0 0

Post a comment