Over the past three weeks, we’ve looked at how to get started using JavaScript on your website, how to implement jQuery for animations and effects, and how to use JavaScript objects as part of this series.
Now we’re going back to basics. In order to actually use JavaScript, you need to know how to load it onto your site.
This is the fourth post in our five-part series focusing on JavaScript for WordPress developers. Throughout this series, you’ll learn the basics, but I’ll presume you already have a working knowledge of HTML and CSS. If you need help with these building blocks, take a look at our series about WordPress Development for Beginners.
So far in this series we’ve looked at the basics of JavaScript, variables, arrays, functions, objects and we’ve sunk our teeth into basic jQuery. Now it’s time to dig a little deeper and start to understand how jQuery fits together with WordPress and how you can load scripts while maintaining their dependencies.
How WordPress Loads Scripts
Let’s take a step back and first look at how WordPress loads scripts and styles. We’ve learned that when creating JavaScript files you can add them to your page in the head section or in the body section, preferably at the bottom.
WordPress does the exact same thing but you can’t (read: shouldn’t) just plop these script tags in the header or footer file of your theme. WordPress uses an intelligent loading mechanism called enqueueing that figures out which files to load and when.
This mechanism is needed to make sense of dependencies. If you write some jQuery code for your theme, jQuery itself needs to be loaded first. You may write code that relies on a jQuery plugin. In this case, jQuery must be loaded first, then the plugin, and then your code.
Enqueueing Scripts
You can enqueue scripts in your theme’s functions file or in a plugin file. Here’s the full code for including a script that relies on jQuery:
.gist table { margin-bottom: 0; }
Firstly, a function must be hooked into WordPress. The first argument of the add_action()
function tells WordPress where these scripts should be placed: in the front-end of the website or in the admin. If you use wp_enqueue_scripts
they will be placed on the user-facing side, if you use admin_enqueue_scripts
they will be placed in the admin.
Within the hooked function you can use wp_enqueue_script
to add the scripts you need. The function takes one required and four optional parameters:
- The first parameter is the name of your script. You can choose any you like but make sure to name it something unique to prevent naming issues.
- The second parameter should contain the location of the script file.
- The third parameter contains an array of dependencies. In the example above I’ve indicated that jQuery is a dependency. All dependencies are loaded before the script.
- The fourth parameter is a version number
- The fifth and final parameter indicates whether you want to load the script in the header or at the very end of the body section. Use
true
to load at the end, orfalse
(or omit the value) to load it in the head section.
Dependencies
WordPress ships with a copy of jQuery, which is why you can add it as a dependency without any hassle. There are a great many other scripts and jQuery plugins WordPress contains. If your code depends on any of them don’t include your own copy, just add it as a dependency. See this script list in the WordPress Codex to see which ones you can use.
If you are including multiple scripts of your own making you can add them as dependencies just the same. Take a look at the example below:
.gist table { margin-bottom: 0; }
Note that since the base script depends on jQuery the extension script depends on it as well. Regardless, you do not need to add jQuery as a dependency for both. This makes script management a lot easier!
Registering & Enqueueing
Adding a script actually consists of two steps but if you use the wp_enqueue_script()
as shown it is condensed into one. In reality, the first is registered with WordPress, which means that WordPress will “know” about the script. The second step is enqueueing when it is actually added to the page.
In some cases you may want to do them separately to ensure efficient loading of scripts. I’ll show you some neat use-cases shortly.
To register and enqueue in two steps simply use the wp_register_script()
with the exact same parameters as you used wp_enqueue_script()
, then use wp_enqueue_script()
with a single parameter, the script name (sometimes called the handle.)
.gist table { margin-bottom: 0; }
If you’re registering and enqueueing in the same place it doesn’t really make sense but as you’ll see in a moment, sometimes you can register in one function and enqueue in another.