ZeroToWP

Enqueue

Quick Definition

Enqueue is the WordPress method of properly loading JavaScript and CSS files on your site. Instead of hardcoding script tags, you register and queue assets so WordPress handles dependencies, versioning, and placement.

WordPress Enqueuing Scripts documentation in the Plugin Handbook on developer.wordpress.org

What Is Enqueue?

To enqueue in WordPress means to register a JavaScript or CSS file with WordPress and tell it when and where to load that file. Instead of pasting <script> or <link> tags directly into your theme's header, you use WordPress's built-in system to queue assets properly.

WordPress provides two core functions for this:

  • wp_enqueue_script() — Loads a JavaScript file
  • wp_enqueue_style() — Loads a CSS stylesheet

Both functions accept the same key parameters:

  • Handle — A unique name to identify the file (e.g., my-custom-script)
  • Source — The URL of the file, typically generated with plugins_url() or get_template_directory_uri()
  • Dependencies — An array of other scripts or styles that must load first (e.g., ["jquery"])
  • Version — A version number for cache-busting (e.g., 1.0.0). When you update the file, change the version so browsers fetch the new copy instead of serving a cached one
  • In footer (scripts) — Load in the footer instead of the header for better page speed

You call these functions inside the wp_enqueue_scripts hook in your theme's functions.php or in a plugin:

function my_theme_assets() {
    wp_enqueue_style( 'main-css', get_template_directory_uri() . '/style.css', [], '1.0.0' );
    wp_enqueue_script( 'main-js', get_template_directory_uri() . '/js/app.js', ['jquery'], '1.0.0', true );
}
add_action( 'wp_enqueue_scripts', 'my_theme_assets' );

There are different hooks depending on where the assets should load:

  • wp_enqueue_scripts — Front-end pages (the one you will use most)
  • admin_enqueue_scripts — Admin dashboard pages
  • login_enqueue_scripts — The login page

Enqueue in Practice

Every well-built WordPress theme and plugin uses the enqueue system. It solves several problems that hardcoding script tags creates:

  • Dependency management — If your script needs jQuery, WordPress loads jQuery first automatically
  • No duplicates — If two plugins both enqueue jQuery, WordPress only loads it once
  • Cache-busting — Version numbers ensure browsers load updated files
  • Deferred/async loading — Since WordPress 6.3, you can pass defer or async as loading strategies for better performance

You can also use wp_register_script() and wp_register_style() to register an asset without loading it immediately — then conditionally enqueue it only on specific pages where it is needed. This keeps your site lean.

Why It Matters

Properly enqueuing assets is a fundamental WordPress development skill. It prevents conflicts between plugins, improves page load speed, and ensures your scripts and styles work reliably across any WordPress installation. If you ever see a plugin adding raw script tags to the header, that is a red flag — the enqueue system exists for a reason.

Sources

Related Terms

Related Articles