1

Most Efficient Way of Including jQuery in your WordPress Theme


wordpress_logo_3dWordPress by default comes bundled with the jQuery framework. You can find it in the wp-includes\js\jquery directory. Many themes use this framework extensively for various effects and widgets alike. Unless you are on a dedicated server or are using a CDN, chances are that jQuery takes a whole lot of time to load. The best way is to speed up this process is to include the jQuery library fomr the Google’s Ajax Libraries API. Read on to learn how to include it in your theme.

Actually the method is quite simple. If your theme includes a direct jQuery call from the header.php (usually will be written like

<script type=’text/javascript’ src=’http://domain-name.tld/wp-includes/js/jqueryjquery.min.js?ver=2.9.2′></script>

or something similar) just delete the line.

If your theme designer had adopted efficient programming practices, he would have used the wp_enqueue_script() function. It can be identified by going to your Theme Functions (functions.php) file of your theme.

Now to include the script from Google`s Ajax Libraries API servers, just insert the following code into the Theme Functions (functions.php) file of your theme:

// smart jquery inclusion
if (!is_admin()) {
wp_deregister_script(‘jquery’);
wp_register_script(‘jquery’, (“http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js”), false);
wp_enqueue_script(‘jquery’);
}

Once done, save the file and your done. To check if the change has occurred, load your blog’s home page (or any other page for that matter). Now press Ctrl+Shift+R to force refresh the page.

Now take a look at the page source (Ctrl+U or View => Source). If the following line appears, you have successfully included this hack into your wordpress blog:

<script type=’text/javascript’ src=’http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js?ver=2.9.2′></script>

The advantage of this hack is that your page loading times will get faster and in lieu better!


Filed in: How-to, Tips, Wordpress Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

Related Posts

Bookmark and Promote!

One Response to "Most Efficient Way of Including jQuery in your WordPress Theme"

  1. silynnie says:

    Great articles & Nice a site?.

Leave a Reply

Submit Comment

© 2012 Tech With Ric. All rights reserved. XHTML / CSS Valid.