Developer · Guide
How to Enqueue Scripts and Styles the Right Way

Short answer
To enqueue scripts and styles in WordPress, write a function that calls wp_enqueue_script for JavaScript and wp_enqueue_style for CSS, then hook that function to wp_enqueue_scripts with add_action. Give each file a unique handle and its file path, and list any dependencies so WordPress loads them first. Use admin_enqueue_scripts for admin pages and login_enqueue_scripts for the login page.
Key takeaways
- wp_enqueue_scripts is the proper hook for front-end scripts and styles, introduced in WordPress 2.8.0.
- wp_enqueue_style needs a unique handle and a src at minimum, and plugins should use plugins_url for the file path.
- wp_enqueue_script takes handle, src, deps, ver, and args, and it is the recommended way to link JavaScript.
- As of WordPress 6.3, the args value can set a defer or async loading strategy.
- Use admin_enqueue_scripts for admin pages and login_enqueue_scripts for the login page.
What does it mean to enqueue scripts and styles in WordPress?
Enqueuing means telling WordPress to load a script or stylesheet through its own system instead of adding the file by hand.
Developer WordPress states that wp_enqueue_scripts is the proper hook for scripts and styles meant for the front end, even though the name mentions scripts only (checked November 2026).
WordPress then adds each file at the right point in the page. That keeps the load order in one place.
Developer WordPress states that the hook was introduced in WordPress 2.8.0 (checked November 2026).
Learn WordPress states that each file gets a handle, which is a unique name WordPress uses to track it (checked November 2026).
You pick the handle when you enqueue the file. Use a short name that names your theme or plugin, such as “my-theme-main” for a theme file.
Why should you enqueue instead of adding files directly?
You should enqueue because WordPress then controls the load order, avoids duplicate files, and keeps themes and plugins from clashing.
Developer WordPress states that wp_enqueue_script is the recommended method of linking JavaScript to a WordPress page (checked November 2026).
Use that method for every JavaScript file you add. It gives WordPress the handle, the file path, and the list of files that must load first.
Developer WordPress states that the function registers the script if a src is provided and does not overwrite it, then enqueues it (checked November 2026).
That guard stops a second call with the same handle from swapping in a different file by mistake. If you need new values, you must remove the old registration first.
If slow files are your main worry, a guide on how to speed up WordPress covers the wider speed checks to run after you fix loading.
What do you need before you start?
You need access to your theme or plugin files, a text editor, and a way to upload the changed file to your site.
Gather these four items before you write any code.
- The theme or plugin file you will edit, such as the theme functions file or the plugin main file.
- The path to each CSS or JavaScript file you want to load.
- A unique handle for each file, such as “my-plugin-style” or “my-theme-script”.
- A front-end page where you can check the result after you save.
Plan 15 to 30 minutes for one stylesheet and one script, plus time to test the page. Add more time if you need to pass data from PHP to JavaScript.
Work on a copy of the file first if your site is live. Save the old file so you can put it back if a typo breaks the page.
This job is done in code, not on a settings screen. There is no dashboard switch that enqueues a file for you.
How do you enqueue a stylesheet on the front end?
Enqueue a front-end stylesheet by calling wp_enqueue_style inside a function hooked to wp_enqueue_scripts.
Learn WordPress states that wp_enqueue_style needs at minimum a handle and a src, where the handle is a unique name and the src is the full URL or a path relative to the WordPress root (checked November 2026).
Give the handle first and the file path second. WordPress uses the handle to tell your file apart from every other stylesheet.
Learn WordPress states that a plugin must enqueue its files to add custom CSS or JavaScript, and it must register a callback on the wp_enqueue_scripts action hook (checked November 2026).
That rule applies to themes as well when the file belongs on the front end. Hook the callback first, then call the enqueue function inside it.
Follow these steps to add one stylesheet.
- Open your theme functions file or your plugin main file in a text editor.
- Write a small function that calls wp_enqueue_style with your handle and file path.
- Hook that function to wp_enqueue_scripts with add_action.
- Save the file and upload it if you edited a local copy.
- Open a front-end page and check that the stylesheet loads.
Here is a plain code example for a theme stylesheet.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_files' );
function my_theme_enqueue_files() {
wp_enqueue_style( 'style-name', get_stylesheet_uri() );
}
Learn WordPress states that WordPress adds the stylesheet to a wp_styles object and outputs a style element in the head, using the handle as the id and the src as the href (checked November 2026).
That output is what you look for when you check the page source. Find the handle as the id and the file path as the href.
Learn WordPress states that a plugin should use plugins_url to get the plugin directory URL when it enqueues its own files (checked November 2026).
Use that function for plugin files instead of writing the full site address by hand. Here is a plain code example for a plugin stylesheet.
add_action( 'wp_enqueue_scripts', 'my_plugin_enqueue_files' );
function my_plugin_enqueue_files() {
wp_enqueue_style( 'my-plugin-style', plugins_url( 'style.css' ) );
}
How do you enqueue a JavaScript file on the front end?
Enqueue a front-end JavaScript file by calling wp_enqueue_script inside a function hooked to wp_enqueue_scripts.
Developer WordPress states that wp_enqueue_script takes five values in order: handle, src, deps, ver, and args (checked November 2026).
The handle names the script. The src gives the file path. The deps value lists files that must load first.
The ver value sets the version. The args value sets load options.
Developer WordPress states that this example loads a script from a theme: wp_enqueue_script with the handle “script-name”, the path get_template_directory_uri with “/js/example.js”, an empty deps list, the version “1.0.0”, and true as the last value (checked November 2026).
Use that shape as your starting point. Change the handle, the file path, and the version to fit your own file.
Follow these steps to add one script.
- Open your theme functions file or your plugin main file in a text editor.
- Write a small function that calls wp_enqueue_script with your handle, file path, deps, version, and args.
- Hook that function to wp_enqueue_scripts with add_action.
- Save the file and upload it if you edited a local copy.
- Open a front-end page and check that the script loads.
Here is a plain code example for a theme script.
add_action( 'wp_enqueue_scripts', 'my_theme_enqueue_files' );
function my_theme_enqueue_files() {
wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}
A guide on how to defer JavaScript in WordPress explains the loading strategy options in more depth.
How do you register a script first and enqueue it later?
Register a script first when you want WordPress to know about it now and load it only when another file or page needs it.
Developer WordPress states that wp_register_script registers a script to be enqueued later, and scripts listed as a dependency load automatically before it (checked November 2026).
That is the main gain from registering first. Name a registered script as a dependency, and WordPress loads it before the file that needs it.
Developer WordPress states that registering is not strictly necessary but is highly recommended (checked November 2026).
Register files you reuse in more than one place. It keeps the handle, the path, and the version in one spot.
Follow these steps to register and then enqueue.
- Open your theme functions file or your plugin main file in a text editor.
- Call wp_register_script with the handle, file path, deps, version, and args.
- Call wp_enqueue_script with the same handle when the file must load.
- Save the file and upload it if you edited a local copy.
- Open a front-end page and check that the script loads once, not twice.
Here is a plain code example for register and enqueue.
add_action( 'wp_enqueue_scripts', 'my_theme_register_files' );
function my_theme_register_files() {
wp_register_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
wp_enqueue_script( 'script-name' );
}
Developer WordPress states that if you register or enqueue an already registered handle with different values, the new values are ignored, so use wp_deregister_script and register again (checked November 2026).
That rule matters when you change a version or a file path. Change the values in the first registration, or remove the old registration and add it again with the new values.
How do you add defer or async loading and pass data to JavaScript?
Use the args parameter to set a defer or async strategy, and use wp_localize_script to pass PHP values to JavaScript.
Developer WordPress states that as of WordPress 6.3, the args value replaces the prior in_footer value and can set a loading strategy of defer or async (checked November 2026).
Use args when you enqueue a script on WordPress 6.3 or later. Pick defer or async as the strategy value in args.
MDN Web Docs states that a script with the defer attribute runs after the document has been parsed, but before the DOMContentLoaded event fires (checked November 2026). Scripts with defer run in the order they appear in the document.
web.dev states that async downloads a script while the page keeps parsing, then blocks parsing while the script runs once it is ready (checked November 2026). Defer waits to run the script until document parsing is complete.
Developer WordPress states that the dependency tree is taken into account and the most eligible loading strategy is applied (checked November 2026).
WordPress looks at the full list of dependencies before it picks a strategy. A script that other files need may get a different strategy than a script with no dependents.
A guide on how to eliminate render-blocking resources in WordPress covers related fixes for files that hold up page display.
Developer WordPress states that wp_localize_script passes PHP values, such as the AJAX URL and a nonce, to JavaScript as a global object (checked November 2026).
Use it when your script needs a value that only PHP can supply. The AJAX URL and a nonce are two values named in that source.
Call wp_localize_script with the script handle, a name for the global object, and the values to pass. Use the same handle you used when you enqueued the script, so WordPress ties the data to that script.
How do you enqueue files for admin pages and the login page?
Use admin_enqueue_scripts for admin pages and login_enqueue_scripts for the login page instead of the front-end hook.
Developer WordPress states that admin pages use admin_enqueue_scripts and the login page uses login_enqueue_scripts (checked November 2026).
Hook each file to the hook that matches where it must load. A front-end hook will not cover an admin page, and an admin hook will not cover the login page.
| Hook or function | Use it for | Key point |
|---|---|---|
| wp_enqueue_scripts | Front-end scripts and styles | Proper hook for front-end files |
| admin_enqueue_scripts | Admin pages | Separate hook for admin pages |
| login_enqueue_scripts | Login page | Separate hook for the login page |
| wp_enqueue_style | Stylesheets | Needs handle and src at minimum |
| wp_enqueue_script | JavaScript files | Recommended method to link JavaScript |
| wp_register_script | Scripts to load later | Dependency files load first |
Follow these steps to add an admin stylesheet.
- Open your plugin main file in a text editor.
- Write a small function that calls wp_enqueue_style with your handle and file path.
- Hook that function to admin_enqueue_scripts with add_action.
- Save the file and upload it if you edited a local copy.
- Open an admin page and check that the stylesheet loads.
Here is a plain code example for an admin stylesheet.
add_action( 'admin_enqueue_scripts', 'my_plugin_admin_files' );
function my_plugin_admin_files() {
wp_enqueue_style( 'my-plugin-admin-style', plugins_url( 'admin.css' ) );
}
A guide on how to reduce unused JavaScript in WordPress explains why loading a file only where it is needed matters.
Use the admin hook for admin files and the login hook for login files. That keeps front-end files off admin pages and admin files off the front end.
How do you check that your files loaded correctly?
Check that your files loaded by opening a front-end page, viewing the page source, and looking for your handle and file path.
Open a front-end page in your browser. View the page source, then search for your handle. The search should find the file path for the script or stylesheet you added.
Learn WordPress states that WordPress outputs the stylesheet in the head, using the handle as the id and the src as the href, so search the page source for those two values (checked November 2026).
For a script, search for the handle and the file path in the same way. The file path should match the src you passed to the enqueue function.
If the file is missing, check the handle spelling first, then the file path, then the hook name. A small typo in any of those three stops the file from loading.
If the file loads on the wrong pages, check the hook you used. Move the call to admin_enqueue_scripts for admin pages or to login_enqueue_scripts for the login page.
What mistakes should you avoid?
Avoid hard-coding file links, reusing handles, and loading front-end files on every admin page.
Developer WordPress states that new values for an already registered handle are ignored, so a reused handle can leave the old file in place (checked November 2026).
Pick a fresh handle for each new file. If you must change a registered file, remove the old registration and add it again with the new values.
Developer WordPress states that admin pages use admin_enqueue_scripts, so check the hook when a file loads on the front end but not in the admin area (checked November 2026).
Use the hook that matches the page type. That one check fixes many missing admin files.
Avoid adding a file link by hand in a theme template instead of enqueueing it. WordPress cannot track the handle or the dependencies for a hand-added file.
Frequently asked questions
Do I need to register a script before I enqueue it?
No. Registering is not strictly necessary, but it is highly recommended. Register first when you want WordPress to know about a script now and load it later, or when other files list it as a dependency. You can also enqueue a script in one call by giving wp_enqueue_script a src.
What is a handle in WordPress enqueueing?
A handle is the unique name you give a script or stylesheet when you enqueue it. WordPress uses that name to track the file, to check its dependencies, and to output it in the page. Pick a short, exact name for each file, and do not reuse a handle for a different file.
Can I use the same hook for admin pages and the login page?
No. Use admin_enqueue_scripts for admin pages and login_enqueue_scripts for the login page. The front-end hook wp_enqueue_scripts is for scripts and styles meant for the front end. Hook each file to the hook that matches where it must load.
How do I pass data from PHP to JavaScript?
Use wp_localize_script with the script handle, a name for the global object, and the values to pass. It passes PHP values, such as the AJAX URL and a nonce, to JavaScript as a global object. Use the same handle you used when you enqueued the script.
Why is my script not loading after I enqueue it?
Check the handle spelling first, then the file path, then the hook name. A small typo in any of those three stops the file from loading. Also check that a reused handle is not keeping old values in place, since new values for an already registered handle are ignored.
Sources
- wp_enqueue_scripts Developer WordPress
- Enqueuing CSS or JavaScript Learn WordPress
- wp_enqueue_script() Developer WordPress
- wp_register_script() Developer WordPress
- Enqueuing Developer WordPress
- HTML script element MDN Web Docs
- Load third-party JavaScript web.dev
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.


