Using jQuery With WordPress: A Quick Crash Course and Performance Tips

We’re going to be starting a series on innovative ways to use jQuery with WordPress and BuddyPress. Before starting we should look at a couple of basics.  The first foundational item you will need in order to start adding jQuery effects to your WordPress site(s) is how to include jQuery in your theme. If you’re developing a straight XHTML / CSS site, then the process is fairly straightforward. However, different CMS’s have various dynamic ways of calling JavaScript libraries into the site’s theme.  Usually WordPress will load the version of jQuery included in /wp-includes/  by calling:

1
wp_enqueue_script('jquery');

There’s nothing wrong with this, but if you’d rather tap into Google’s AJAX Libraries API to increase your site’s performance, here’s how you do it:

How to Load jQuery From the Google Ajax Library

Copy and paste this into your functions.php file:

1
2
3
4
5
6
7
8
9
function my_init() {
if (!is_admin()) {
// comment out the next two lines to load the local copy of jQuery
wp_deregister_script('jquery');
wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2');
wp_enqueue_script('jquery');
}
}
add_action('init', 'my_init');

Source: Eric Martin of ericmmartin.com

Google keeps these files indefinitely, so you don’t have to worry about them one day disappearing and not being available for your site. I recommend using Google libraries, whether through a plugin or by adding to your theme’s functions. Anything that will make your site even the slightest bit faster will help you to gain a competitive edge.

Add Google Libraries to WordPress Using a Plugin

If you prefer the plugin option, then you can get the same function as above plus whatever options your plugin provides. Use Google Libraries supports a number of JavaScript libraries and lists the following as benefits for performance:

  • increases the chance that a user already has these files cached
  • takes load off your server
  • uses compressed versions of the libraries (where available)
  • Google’s servers are set up to negotiate HTTP compression with the requesting browser

How to Load jQuery in the Footer

Add this to your functions.php files:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
function my_init() {
    if (!is_admin()) {
        wp_deregister_script('jquery');

        // load the local copy of jQuery in the footer
        wp_register_script('jquery', '/wp-includes/js/jquery/jquery.js', false, '1.3.2', true);

        // or load the Google API copy in the footer
        //wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js', false, '1.3.2', true);

        wp_enqueue_script('jquery');
    }
}
add_action('init', 'my_init');

This is what the function above does:

  1. It de-registers the local copy of jQuery that is ordinarily used by WordPress
  2. It registers WordPress’ copy of jQuery and places it in the footer.
  3. OR It calls the Google Library to load jQuery and places it in the footer. (if you uncomment that section)

After you do these things, then you’re safe to register your other external JavaScript files that you’re using.

The reason for loading jQuery in the footer is that it isn’t usually any use until all the content has been loaded anyway. Why make your users wait while your site loads all of your scripts before your content? The server can only load so many requests at a time from a website. Unless jQuery is modifying the DOM as part of its functionality, you’re safe to put your scripts in the footer.

This post should be enough to get you started with playing with jQuery and WordPress. Stay tuned for some more jQuery tutorials next week for enhancing your WordPress and BuddyPress sites!

Featured Plugin - WordPress Ecommerce Shopping Cart Plugin

Out of all the WordPress ecommerce plugins available, MarketPress has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Featured Plugin - WordPress Facebook Plugin

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Featured Plugin - WordPress Pop-Up Chat Plugin

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Featured Plugin - WordPress Google Maps Plugin

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

Featured Plugin - WordPress Wiki Plugin

To get a wiki up and running you used to need to install Mediawiki and toil away for days configuring it... not any more! This plugin gives you *all* the functionality you want from a wiki, in WordPress!!!
Find out more

Featured Plugin - WordPress Q&A Site Plugin

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Featured Plugin - WordPress Newsletter Plugin

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more

Featured Plugin - WordPress Membership Site Plugin

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

Featured Plugin - WordPress Appointments Plugin

Take, set and manage appointments and client bookings without having to leave WordPress. Appointments+ makes it easy.
Find out more

Comments (5)

  1. Hi there! When trying at my site, it fails with an “Fatal error: Call to undefined function add_action() in /home/***/public_html/wp-includes/functions.php” error where “add_action(‘init’, ‘my_init’);” line is.

    Any suggestion?

  2. Hi,
    Thanks for that Sarah. I have been searching for a definitive guide to installing jquery into wordpress all morning.

    For example, where do I put a local function?

    A lot of the articles don’t spell out like a two year old that I am what should go where. Do you think if you find the time you could do an article about this?
    Please pretty please?

    :-)

Participate