Add jQuery to WordPress

By default WordPress comes packaged with a jQuery function but because the jQuery libraries are updated constantly I decided to call my own jQuery libraries from a CDN. CDN stands for content delivery network, large enterprises have their own CDN’s where they host copies of popular script. In the following example I’m using the CDN of Google. What I’m going to do is make a function for functions.php that unloads the default libraries, registers the new and updated ones and makes them available for use.
Copy the following function to your functions.php within your theme directory. Make sure this function is pasted within the php tags.

function my_jquery() {
if ( !is_admin() ) { // This makes sure the updated libraries are not used within the admin area to avoid conflicts
 wp_deregister_script('jquery');                                                                             // using wp_deregister_script() to disable the version that comes packaged with WordPress
 wp_deregister_script('jquery-ui-core');
 wp_deregister_script('jquery-ui-tabs');
 wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js');             // using wp_register_script() to register updated libraries (this example uses the CDN from Google but you can use any other CDN or host the scripts yourself)
 wp_register_script('jquery-ui-core', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js');
 wp_enqueue_script('jquery');                                                                                // using wp_enqueue_script() to load the updated libraries
 wp_enqueue_script('jquery-ui-core');
 wp_enqueue_script('jquery-ui-tabs');
 wp_enqueue_script('jquery-ui-accordion');
 }
}
add_action('init', 'my_jquery'); // load the function

Revisions

There are no revisions for this post.

Tags: ,

No comments yet.

Leave a Reply