WordPress GuideDevelopment → Enqueue Scripts

How to enqueue scripts using wp_enqueue_scripts

If you’ve ever copied a <script> tag directly into your WordPress theme’s header or footer file, stop right there. WordPress has a better way.

To load JavaScript files the right way—and avoid conflicts, duplicate loading, or performance issues—you should use wp_enqueue_script. Let’s walk through how it works, when to use it, and how to make sure your scripts are loaded exactly where and when you need them.

Get fast, reliable hosting for WordPress

Power your site with the industry’s fastest, most optimized WordPress hosting

Understanding wp_enqueue_scripts in WordPress

Polyfills are JavaScript files that mimic modern browser features in older browsers. WordPress includes a built-in polyfill script (wp-polyfill) to ensure compatibility with outdated environments—especially browsers that don’t support features like Promise, Array.prototype.includes, or Object.assign.

This function should go inside your theme’s functions.php file or inside a plugin. For admin area scripts, you’d use a separate hook called admin_enqueue_scripts.

Basic usage of wp_enqueue_script

At its core, wp_enqueue_script looks like this:

Here’s what each parameter means:

How to enqueue local JavaScript files

For scripts stored in your theme folder, use get_template_directory_uri() (for parent themes) or get_stylesheet_directory_uri() (for child themes):

Good practices:

How to enqueue external scripts (like CDNs)

To load a script from a CDN, just use the direct URL in place of the file path:

Only do this if you need a different version than the one WordPress provides. If you’re using built-in jQuery, you can enqueue it like this:

How to enqueue JavaScript conditionally

Want to load a script only on your contact page? Use conditional logic:

This improves performance by only loading what you need.

Enqueueing scripts with dependencies

You can load scripts in a specific order by setting dependencies. For example:

WordPress knows to load jQuery first. Common built-in dependencies include:

How to load scripts in the WordPress admin

For backend admin pages, use the admin_enqueue_scripts hook. You can even check for specific admin pages using the $hook parameter:

This ensures your admin script loads only where it’s needed.

Adding version control to avoid browser cache issues

If users report that changes to your script aren’t showing, browser cache is likely to blame. You can use PHP’s filemtime() function to dynamically version your scripts:

This changes the version each time the file is updated.

How to debug enqueued scripts in WordPress

Sometimes a script doesn’t load, and you’re left scratching your head. Here’s how to troubleshoot:

These methods help you confirm whether the script was enqueued and loaded successfully.

wp_enqueue_scripts FAQs

wp_enqueue_script helps load JavaScript files the WordPress way. It ensures your scripts are included only once, loaded in the correct order, and placed in either the header or footer based on performance needs.

wp_register_script lets you register a script for later use without loading it. wp_enqueue_script actually queues it up to load. If needed, you can register a script first and conditionally enqueue it later.

Use wp_enqueue_style like this:

You can enqueue additional styles from your theme folder or external stylesheets using this function in the same wp_enqueue_scripts hook.

WP Mail SMTP is one of the most popular and reliable SMTP plugins. It supports multiple mailers like Gmail, SendLayer, and SMTP.com. Other good options include Post SMTP and Easy WP SMTP.

Ready to get started?

Get the fastest, most secure WordPress.org hosting on the market.

Additional resources

What is managed WordPress hosting? →

Get details and decide if managed WordPress hosting is right for you.

How to push specific pages in WordPress →

Easily push specific pages from staging to live in WordPress without affecting the entire site.














A complete guide to WordPress shortcodes →

Shortcodes make life easier. Learn how to get started!

Trust us to help you choose the ideal hosting solution

Loading form…