Table of Contents
If you’re a WordPress developer, chances are you’ve come across the wp_enqueue_scripts hook. This hook is a key component of WordPress theme and plugin development, and it allows you to properly add scripts and styles to your WordPress site.
But if you’re new to WordPress development, the wp_enqueue_scripts hook can seem a bit confusing. In this article, we’ll break down exactly what wp_enqueue_scripts does, and how you can use it to properly enqueue your assets in WordPress.
What is the wp_enqueue_scripts hook?
The wp_enqueue_scripts hook is an action hook that is called by the WordPress core when it is time to load scripts and styles. It is typically used in conjunction with the wp_register_script and wp_register_style functions, which register scripts and styles respectively.
The wp_enqueue_scripts hook is called in the wp_head hook, which is itself called by the wp_footer hook. This means that the wp_enqueue_scripts hook is called relatively late in the loading process, after most of the HTML for the page has been generated.
Why use the wp_enqueue_scripts hook?
There are a few reasons why you should use the wp_enqueue_scripts hook to enqueue your scripts and styles:
- Proper script and style loading order: Using the
wp_enqueue_scriptshook ensures that your scripts and styles are loaded in the correct order. This is especially important if your scripts or styles depend on other scripts or styles being loaded first. - Eliminates the need for hardcoded
<script>and<link>tags: Using thewp_enqueue_scriptshook allows you to avoid hardcoding<script>and<link>tags in your theme or plugin code. This makes it easier to maintain your code, as you don’t have to worry about tracking down and updating all the hardcoded tags if you need to make a change. - Enables script and style dependencies: When you enqueue a script or style using the
wp_enqueue_scriptshook, you can specify dependencies that must be loaded first. This is a powerful tool that allows you to ensure that your scripts and styles are only loaded when they are needed, and that they are loaded in the correct order.
How to enqueue scripts and styles using wp_enqueue_scripts
Enqueuing scripts and styles using the wp_enqueue_scripts hook is relatively straightforward. Here’s the basic process:
- Register the script or style: Before you can enqueue a script or style, you must first register it using the
wp_register_scriptorwp_register_stylefunction. This function takes a few arguments, including the handle (a unique identifier for the script or style), the URL of the script or style file, and an array of dependencies (optional).
Here’s an example of how to register a script:
wp_register_script( 'my-script', 'http://example.com/path/to/my-script.js', array('jquery'), '1.0', true );
The first argument is the handle for the script, which must be unique. The second argument is the URL of the script file. The third argument is an array of dependencies, which are other scripts that this script depends on. In this case, the script depends on the ‘jquery’ script. The fourth argument is the version number for the script, which can be useful for cache busting. The final argument specifies whether the script should be loaded in the footer (true) or the header (false).
Here’s an example of how to register a style:
wp_register_style( 'my-style', 'http://example.com/path/to/my-style.css', array(), '1.0', 'all' );
The first argument is the handle for the style, which must be unique. The second argument is the URL of the style file. The third argument is an array of dependencies, which are other styles that this style depends on. The fourth argument is the version number for the style, which can be useful for cache busting. The final argument specifies the media type for the style (e.g. ‘all’, ‘screen’, ‘print’).
- Enqueue the script or style: Once you’ve registered a script or style, you can enqueue it using the
wp_enqueue_scriptorwp_enqueue_stylefunction. These functions take a single argument: the handle for the script or style that you want to enqueue.
Here’s an example of how to enqueue a script:
wp_enqueue_script( 'my-script' );
And here’s an example of how to enqueue a style:
wp_enqueue_style( 'my-style' );
It’s important to note that you should only enqueue scripts and styles that you’ve previously registered using wp_register_script or wp_register_style.
Enqueuing scripts and styles in the WordPress backend
In addition to the frontend of your WordPress site, you may also want to enqueue scripts and styles in the WordPress backend (e.g. the admin area). To do this, you can use the admin_enqueue_scripts hook.
The process for enqueuing scripts and styles in the backend is the same as in the frontend. First, you’ll need to register the script or style using wp_register_script or wp_register_style. Then, you can enqueue the script or style using wp_enqueue_script or wp_enqueue_style.
And here’s an example of how to enqueue a style in the backend:
function my_admin_styles() {
wp_register_style( 'my-admin-style', 'http://example.com/path/to/my-admin-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-admin-style' );
}
add_action( 'admin_enqueue_scripts', 'my_admin_styles' );
It’s important to note that the admin_enqueue_scripts hook is only called on admin pages, so your scripts and styles will only be enqueued on the backend.
Enqueuing scripts and styles in plugin development
If you’re developing a plugin, you’ll also need to enqueue your scripts and styles. The process is the same as in theme development, but you’ll need to use the wp_enqueue_scripts hook in your plugin instead of in your theme.
Here’s an example of how to enqueue a script in a plugin:
function my_plugin_scripts() {
wp_register_script( 'my-plugin-script', 'http://example.com/path/to/my-plugin-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-plugin-script' );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_scripts' );
And here’s an example of how to enqueue a style in a plugin:
function my_plugin_styles() {
wp_register_style( 'my-plugin-style', 'http://example.com/path/to/my-plugin-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-plugin-style' );
}
add_action( 'wp_enqueue_scripts', 'my_plugin_styles' );
Enqueuing scripts and styles in WordPress child themes
If you’re using a child theme, you’ll need to be careful about enqueuing scripts and styles. If you enqueue a script or style in your child theme using the same handle as a script or style in the parent theme, you’ll overwrite the parent theme’s script or style.
To avoid this, you should either use a different handle for your script or style, or you should use the wp_deregister_script or wp_deregister_style function to remove the parent theme’s script or style before enqueuing your own.
And here’s an example of how to enqueue a style in a child theme while removing the parent theme’s style:
function my_child_styles() {
wp_deregister_style( 'parent-style' );
wp_register_style( 'my-child-style', 'http://example.com/path/to/my-child-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-child-style' );
}
add_action( 'wp_enqueue_scripts', 'my_child_styles', 11 );
It’s important to note that you should use the wp_deregister_script and wp_deregister_style functions with caution. Removing a script or style that is required by other scripts or styles can cause errors and unexpected behavior on your site.
Using external libraries and CDNs
In some cases, you may want to enqueue scripts or styles that are hosted on an external site (e.g. a CDN) instead of on your own server. To do this, you can simply use the URL of the external script or style in the wp_register_script or wp_register_style function.
Here’s an example of how to enqueue a script from an external CDN:
wp_register_script( 'external-script', 'https://cdn.example.com/path/to/external-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'external-script' );
And here’s an example of how to enqueue a style from an external CDN:
wp_register_style( 'external-style', 'https://cdn.example.com/path/to/external-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'external-style' );
Using external libraries and CDNs can help improve the performance of your site, as the scripts and styles will be loaded from a separate server and won’t impact the loading time of your own server.
Enqueuing scripts and styles in the WordPress block editor
If you’re developing a plugin or theme that integrates with the WordPress block editor (formerly known as the Gutenberg editor), you’ll need to enqueue your scripts and styles in the block editor as well.
To enqueue scripts and styles in the block editor, you’ll need to use the enqueue_block_editor_assets hook. This hook is similar to the wp_enqueue_scripts hook, but it is only called in the block editor.
And here’s an example of how to enqueue a style in the block editor:
function my_block_editor_styles() {
wp_register_style( 'my-block-editor-style', 'http://example.com/path/to/my-block-editor-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-block-editor-style' );
}
add_action( 'enqueue_block_editor_assets', 'my_block_editor_styles' );
It’s important to note that the enqueue_block_editor_assets hook is only called in the block editor, so your scripts and styles will only be enqueued in the block editor.
Enqueuing scripts and styles in WordPress customizer
If you’re developing a plugin or theme that integrates with the WordPress customizer, you’ll need to enqueue your scripts and styles in the customizer as well.
To enqueue scripts and styles in the customizer, you’ll need to use the customize_controls_enqueue_scripts hook. This hook is similar to the wp_enqueue_scripts hook, but it is only called in the customizer.
Here’s an example of how to enqueue a script in the customizer:
function my_customizer_scripts() {
wp_register_script( 'my-customizer-script', 'http://example.com/path/to/my-customizer-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-customizer-script' );
}
add_action( 'customize_controls_enqueue_scripts', 'my_customizer_scripts' );
And here’s an example of how to enqueue a style in the customizer:
function my_customizer_styles() {
wp_register_style( 'my-customizer-style', 'http://example.com/path/to/my-customizer-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-customizer-style' );
}
add_action( 'customize_controls_enqueue_scripts', 'my_customizer_styles' );
It’s important to note that the customize_controls_enqueue_scripts hook is only called in the customizer, so your scripts and styles will only be enqueued in the customizer.
Enqueuing scripts and styles in WordPress login form
If you’re developing a plugin or theme that integrates with the WordPress login form, you’ll need to enqueue your scripts and styles in the login form as well.
To enqueue scripts and styles in the login form, you’ll need to use the login_enqueue_scripts hook. This hook is similar to the wp_enqueue_scripts hook, but it is only called in the login form.
Here’s an example of how to enqueue a script in the login form:
function my_login_scripts() {
wp_register_script( 'my-login-script', 'http://example.com/path/to/my-login-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-login-script' );
}
add_action( 'login_enqueue_scripts', 'my_login_scripts' );
And here’s an example of how to enqueue a style in the login form:
function my_login_styles() {
wp_register_style( 'my-login-style', 'http://example.com/path/to/my-login-style.css', array(), '1.0', 'all' );
wp_enqueue_style( 'my-login-style' );
}
add_action( 'login_enqueue_scripts', 'my_login_styles' );
It’s important to note that the login_enqueue_scripts hook is only called in the login form, so your scripts and styles will only be enqueued in the login form.
Advanced techniques for enqueuing scripts and styles
While the basic techniques for enqueuing scripts and styles in WordPress are relatively straightforward, there are also some advanced techniques that you may want to consider using in your projects.
Using dependencies and translations
In the examples we’ve seen so far, we’ve used the array() parameter to specify an array of dependencies for our scripts and styles. This is an important feature of the wp_register_script and wp_register_style functions, as it allows you to specify other scripts or styles that your script or style depends on.
For example, if your script uses jQuery, you should include 'jquery' in the dependencies array. This will ensure that jQuery is loaded before your script, and will prevent any errors or conflicts.
Here’s an example of how to enqueue a script with dependencies:
wp_register_script( 'my-script', 'http://example.com/path/to/my-script.js', array('jquery', 'other-script'), '1.0', true );
wp_enqueue_script( 'my-script' );
You can also use the $in_footer parameter to specify whether your script should be loaded in the <head> or the <footer> of your site. It’s generally recommended to load scripts in the footer to improve the performance of your site.
In addition to dependencies, you can also use the wp_localize_script function to pass data from PHP to your script. This is useful for translating strings in your script, or for passing dynamic data from the server to your script.
Here’s an example of how to use wp_localize_script to pass data to your script:
wp_register_script( 'my-script', 'http://example.com/path/to/my-script.js', array('jquery'), '1.0', true );
$data = array(
'some_string' => __( 'Some string to translate', 'text-domain' ),
'a_value' => '10'
);
wp_localize_script( 'my-script', 'my_script_data', $data );
wp_enqueue_script( 'my-script' );
In this example, the my_script_data object will be available in your script, and you can access the some_string and a_value properties like this:
console.log( my_script_data.some_string ); // 'Some string to translate'
console.log( my_script_data.a_value ); // '10'
Using version numbers
In the examples we’ve seen so far, we’ve used the $ver parameter to specify a version number for our scripts and styles. This is an important feature of the wp_register_script and wp_register_style functions, as it allows you to specify a version number for your script or style.
The version number serves several purposes:
- It allows you to invalidate the cache for your script or style when you make changes to it.
- It helps prevent conflicts with other scripts or styles that may have the same handle.
- It helps you to keep track of the changes you make to your script or style.
To use a version number for your scripts and styles, simply pass a string or number as the $ver parameter when you register or enqueue them. For example:
wp_register_script( 'my-script', 'http://example.com/path/to/my-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-script' );
In this example, the version number for the my-script script is 1.0.
It’s generally recommended to use version numbers for all of your scripts and styles, especially if you’re building a plugin or theme that will be distributed to other users. This will help ensure that your users always have the latest version of your scripts and styles, and will help prevent conflicts with other plugins or themes.
Enqueuing scripts and styles conditionally
In some cases, you may want to enqueue a script or style only under certain conditions. For example, you may want to enqueue a script only on a specific page, or only for users with a certain role.
To enqueue a script or style conditionally, you’ll need to use a conditional statement in your wp_enqueue_scripts action. For example:
function my_scripts() {
if ( is_page( 'contact' ) ) {
wp_register_script( 'my-script', 'http://example.com/path/to/my-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-script' );
}
}
add_action( 'wp_enqueue_scripts', 'my_scripts' );
In this example, the my-script script will only be enqueued on the “Contact” page. You can use any WordPress function or conditional statement to enqueue scripts and styles conditionally.
Enqueuing scripts and styles in the admin area
In some cases, you may want to enqueue a script or style only in the WordPress admin area. For example, you may want to enqueue a script that adds a custom metabox to the post edit screen.
To enqueue a script or style in the admin area, you’ll need to use the admin_enqueue_scripts hook. For example:
function my_admin_scripts() {
wp_register_script( 'my-admin-script', 'http://example.com/path/to/my-admin-script.js', array('jquery'), '1.0', true );
wp_enqueue_script( 'my-admin-script' );
}
add_action( 'admin_enqueue_scripts', 'my_admin_scripts' );
In this example, the my-admin-script script will be enqueued in the WordPress admin area. You can use the $hook_suffix parameter to enqueue scripts and styles conditionally in the admin area, just like you can in the wp_enqueue_scripts hook.
Tips for optimizing your enqueued scripts and styles
Now that you know how to enqueue scripts and styles in WordPress, here are some tips for optimizing your enqueued assets:
- Use version numbers for your scripts and styles to ensure that users always have the latest version and to prevent conflicts with other plugins or themes.
- Use the
$depsparameter to specify dependencies for your scripts and styles, and use the$in_footerparameter to load scripts in the footer to improve performance. - Use the
wp_localize_scriptfunction to pass data from PHP to your scripts, and use thewp_enqueue_scriptsaction to enqueue scripts and styles conditionally. - Use the
wp_dequeue_scriptandwp_dequeue_stylefunctions to remove scripts and styles that are enqueued by other plugins or themes. - Use a plugin or service like Autoptimize or Cloudflare to minify and concatenate your scripts and styles to reduce the number of HTTP requests and improve performance.
By following these tips, you can ensure that your site loads smoothly and performs optimally for your users.
Conclusion
In this article, we’ve covered the basics of enqueuing scripts and styles in WordPress. We’ve discussed how to register and enqueue scripts and styles in the frontend, backend, block editor, customizer, and login form. We’ve also covered how to enqueue scripts and styles from external libraries and CDNs, and how to enqueue scripts and styles in child themes and plugins.
By following these best practices for enqueuing scripts and styles in WordPress, you can ensure that your site loads smoothly and performs optimally for your users.








