How to Use JQuery with a Child Theme in WordPress.

Yet another instalment in our free Tips and Tricks library.

There are many situations within a WordPress website environment when you might want to hook up with a Javascript function to perform a particular function.

For instance:

  • Validating form variables locally before posting to the server or adding to database.
  • Performing calculations.
  • Using JQuery to control CSS to customise page layout.

This example show you how to:

  1. Download the JQuery Library
  2. Use JQuery with a Child Theme.
  3. Enqueue Scripts.
  4. Create a JQuery function and use it.

Disclaimer:

The best case scenario is to always load the JQuery Library from a Content Delivery Network – for maintenance, speed, caching and a host of other reasons.

But this tutorial just concentrates on the basic functionality and works with a site-specific install of the JQuery Library.

What is JQuery?

The explanation on the official JQuery site is as follows:

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.

 

Step 1 :

Download the JQuery Library from the website. Download the compressed, production version (2.2.2 at time of writing this post)

In your Child Theme folder (/wp-content/themes/Your-Child-Theme-Folder), create a folder called js (the name’s not relevant, merely somewhere to store javascript files).

Save the downloaded file into your js folder.

Step 2:

Create a new javascript file (you can either create this on your hard disk and upload it when you’re done, or you can create and edit on-the-fly using either the WordPress editor, or a Plugin like WPIDE / Advanced Code Editor to make it easier for you).

Save this file as script.js in the js folder. (again, the name of this file is not relevant – just so long as you know what it’s name is and where it is).

Step 3:

In order to use JQuery with a Child Theme, you need to enqueue the script. You could also register it first, but as stated earlier, I’m sticking to the simplest implementation here.

One thing you definitely shouldn’t do is to try and add the JQuery script link in the head tag of your page.  It can break your site because plugins or themes that use JQuery can be forced to load twice. So to be clear, don’t do this:

Instead, edit the functions.php file in your Child Theme folder and add the following script within the php tags:
(note that get_stylesheet_directory_uri(); points to the current Theme – or in your case, Child Theme – folder).

Step 4:

Now that you’ve enabled the JQuery Library, you’re going to add some code to the custom script.js file to confirm that it’s properly loaded.

Edit the file and add an event listener that calls a function when the document is properly loaded. The complete Javascript file is as follows:

Here’s a useful link on the topic.