What is WP Enqueue and How Do You Use It?

Last Updated on September 21, 2022 by 8 Comments

What is WP Enqueue and How Do You Use It?
Blog / Resources / What is WP Enqueue and How Do You Use It?

When you first learn HTML, the only way you know how to insert any kind of interactivity is using script tags in the header or footer of the page to set the type and source of an external JavaScript file. (Or if you’re really just learning, the entire JS function between script tags right there in the body itself. That’s fine for learning, but it’s pretty bad practice once you move into advanced WordPress development. Enter the enqueue function.

Once you move past simple, one-page websites, those beginner practices can create some really sticky situations. As you start adding more and more JavaScript, your website gets slower and slower. Updating it takes longer. Eventually, you’ve made a big pile of JavaScript-getti and no matter how hard you try, that pile of noodles ain’t coming untangled.

Thankfully, you can use the enqueue function in WordPress to add styles and scripts that the CMS take care of for you. All the messiness is handled for you. While it’s not as simple as directly pasting the script or styles you want in the header or footer of each page, it’s the right way to handle it. WordPress isn’t terribly opinionated, there is definitely a series of best practices you should know about

And wp_enqueue_scripts is at the forefront of those practices.

What’s the Big Deal?

The primary reason you will want to use wp_enqueue is to keep your site running smoothly and quickly. Page speed is important and using the same scripts and styles over and over and over again doesn’t help. At all.

Luckily, wp_enqueue is an example of functional programming. All that means is that you write a single piece of code to be executed (your JavaScript snippet), and you use wp_enqueue to call it instead of having to rewrite/paste the whole thing every time you need it.

That’s. Awesome.

Even more awesome, using the enqueue method makes WordPress itself insert the _script_ tags into the header and footer where they belong automagically, loading them without having to input them on each page separately.

WP Enqueue Syntax and Params

Enqueueing scripts really isn’t that difficult. If you are savvy enough to work with JavaScript in the first place, the PHP functions you use for enqueueing will be a cinch.

In a typical script tag, you will either import the external .js file directly or paste the entire, long snippet of code between the tags themselves. (This is also what you’re doing if you paste something into the Divi code module or the WP Custom HTML or Text widgets).

You just need to know a few basic parameters and syntax.

The Codex gives this as the base enqueue code:

wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );

All in all, it’s pretty simple to break down into usable bits by parameters.

  • wp_enqueue_script() is the function all that will put all the code in the page where it goes.
  • $handle is a unique name for the script itself.
  • $src represents the URL of the actual .js file you’re enqueueing.
  • $deps are the $handles of any dependencies this one requires.
  • $ver will be the version number. If none is specified, you get the WP installation version added automatically.
  • $in_footer or $in_header tells WordPress where to put your script.

The full documentation to enqueue scripts can be seen in the Codex.

Attention, Students: WP Registration Begins Now!

In addition to _wp_enqueue__, WP has a handy-dandy method called _wp_register__, too. Both methods use the same parameters and syntax, so you really get a two-for-one deal with the pair of them. Essentially, registering a script is the same as naming a function in JavaScript.

While not necessary, registering your scripts can make your life a lot easier because you won’t have to declare them as dependencies later on down the line. They’ll already be registered. Hence…_wp_register__. Once a script is registered, you can call it back by its $handle, as you will see in the example below.

You will register your code like this:

wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js', false, null, $in_footer);

Then, whenever you need it again, you can always call it back like this:


wp_enqueue_script( 'jquery' ); 

Additionally, registration means that you don’t have to load a script even when it’s not needed. WPMU has an awesome example of shortcode creation: if you register the script to a shortcode that uses _wp_enqueue__, it will only get used when the shortcode is. However if you only call it by enqueueing it, it will be loaded even when the shortcode isn’t used.

You can read all about the method in the Codex.

Additionally, plugin authors are required to enqueue scripts if they want included in the WordPress.org repo, so if that’s on your plate…better get to queuein’.

Putting It Together

Implementing the code is easy. Just drop a snippet like the one below into your functions.php. and WordPress handles the rest. I mean, this is a really basic snippet to call jQuery, but you can see how the stuff plays together.

You would normally use something that looks like this:

<script type="text/javascript" src="jquery.js"></script>

Once you learn how use enqueue_scripts_, you will see something more like this:

add_action('wp_enqueue_scripts', 'add_scripts');
function add_scripts(){
    wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js', false, null, true);
    wp_enqueue_script( 'jquery' ); 
}

Now, notice in the code above how it uses the add_scripts()_ function that’s a part of WordPress already to outline the order of the next few steps. First, the wp_register_ function gets all the details specified and puts the script in the page footer, and then uses wp_enqueue to finally call it once everything is set up.

Wrapping Up

That’s really all there is to it. Well, that’s not true–enqueue is a pretty ingrained part of WP. But those are the basics that get you poking around at doing more with WordPress than you may have before.

The basics we all learned of putting in scripts when we started web development still work, but they aren’t always the best way to do things. Part of the beauty of WordPress being built on PHP is that you can bring this kind of logic into your work while still really keeping the same basic structure and workflow as you would have had anyway.

Article thumbnail image by hanss / shutterstock.com

Divi

Want To Build Better WordPress Websites? Start Here! 👇

Take the first step towards a better website.

Get Started
Divi
Premade Layouts

Check Out These Related Posts

Top Features Of Divi Dash For Busy WordPress Agencies

Top Features Of Divi Dash For Busy WordPress Agencies

Posted on October 17, 2024 in Resources

Running a WordPress agency isn’t a walk in the park. It’s a juggling act of keeping clients happy, staying on top of changes, managing your team, building killer websites, and maintaining them — all while trying to stand out in a crowded market. But what if you had a tool to help...

View Full Post
WordPress vs Medium (2024) — Where Should You Blog?

WordPress vs Medium (2024) — Where Should You Blog?

Updated on February 14, 2024 in Resources

If there is one question that goes back to the very beginning of blogging, it’s “what blogging platform should I use?” Everyone asks this question (to Google, most likely), and everyone gets bombarded with a thousand different answers. That’s primarily because there are so...

View Full Post
9 Non-Profit Child Themes for Divi

9 Non-Profit Child Themes for Divi

Updated on January 23, 2023 in Resources

There are lots of nonprofit organizations across the globe. Just about every one of them needs a well-designed website to tell their story and receive donations to help their causes. Divi is an excellent theme for nonprofits such as charities. Fortunately, you don’t have to start from scratch for...

View Full Post

8 Comments

  1. Interesting information. Thanks for making it clear.

  2. Something else that’s great about wp_enqueue is that you can combine them with PHP statements! For example, sometimes there are scripts that run on the home page (perhaps a custom slider script) that I don’t need elsewhere. I can just use an “if” statement to check if_front_page() then load my script!

    function mysite_divi_scripts() {
    wp_enqueue_script( ‘mysite_page-ux’, get_stylesheet_directory_uri() . “/js/page-ux.js”, array(‘jquery’) );

    if ( is_front_page() ) {
    wp_enqueue_script( ‘mysite_divi-carousel’, get_stylesheet_directory_uri() . “/js/divi-carousel.js”, array(‘jquery’), ‘20151215’, true );
    }
    }
    add_action( ‘wp_enqueue_scripts’, ‘mysite_divi_scripts’ );

    This works for other types of WordPress pages as well, such as is_single(), is_archive(), etc.

  3. How much of this is already built into DIVI, and how well does this technique work (and is it needed) with DIVI?

  4. I enqueue the css style sheet within a child themes functions.php, but on some of my sites updates are not reflected due to caching. Is there a way to have the enqueue function pick up the version # of the child themes style.css?

    Thanks for all the information, great topic!

  5. Interesting.

  6. Nice thanks

  7. I’m just a simple web designer. Is this something I need to know and do on all my Divi web sites to make them load faster?

  8. Thanks for the info 🙂

Leave A Reply

Comments are reviewed and must adhere to our comments policy.

Get Started With Divi