wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, array|bool $args = array() )
- Since
- 2.1.0, 6.3.0, 6.9.0, 7.0.0
- Source
wp-includes/functions.wp-scripts.php:473
Load a JavaScript file on a WordPress page with wp_enqueue_script(), declaring its handle, source URL, dependencies, version, and loading strategy. Call it from a wp_enqueue_scripts or admin_enqueue_scripts callback. Since 6.3 the $args array accepts 'strategy' ('defer' or 'async') and 'in_footer'; newer releases add 'fetchpriority' and script module dependencies.
Description
Registers the script if $src provided (does NOT overwrite), and enqueues it.
Compatibility
- WordPress
- since 7.0.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$handlestring- Name of the script. Should be unique.
$srcstringoptional- Full URL of the script, or path of the script relative to the WordPress root directory.
Default empty.Default:'' $depsstring[]optional- An array of registered script handles this script depends on. Default empty array.Default:
array() $verstring|bool|nulloptional- String specifying script version number, if it has one, which is added to the URL as a query string for cache busting purposes. If version is set to false, a version number is automatically added equal to current installed WordPress version.
If set to null, no version is added.Default:false $argsarray|booloptional- An array of extra args for the script. Default empty array. Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.Default:
array()$strategystringOptional. If provided, may be either 'defer' or 'async'.$in_footerbooldefault: 'false'Optional. Whether to print the script in the footer.$fetchprioritystringdefault: 'auto'Optional. The fetch priority for the script.$module_dependenciesarrayOptional. IDs for module dependencies loaded via dynamic import. Default empty array. For the full data format, see the$depsparam of wp_register_script_module(). When provided, the script must either be printed in the footer (within_footerset to true) or use a deferred loadingstrategy(defer), so that the script modules import map is printed before the script is evaluated. Otherwise dynamic imports may fail to resolve.
Code examples
Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.
Enqueue a deferred front-end script
Register and enqueue a plugin script with a dependency, a cache-busting version, and the defer strategy from a wp_enqueue_scripts callback.
add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_assets' );
function myplugin_enqueue_assets() {
wp_enqueue_script(
'myplugin-app',
plugins_url( 'js/app.js', __FILE__ ),
array( 'wp-api-fetch' ),
'1.4.2',
array(
'strategy' => 'defer',
'in_footer' => true,
)
);
}The registration step never overwrites an existing handle, so if another plugin registered 'myplugin-app' first, your $src is silently ignored; pick a prefixed, unique handle.
Pass server data to an enqueued script
Attach a small inline configuration object before the script runs instead of printing globals in the template.
add_action( 'wp_enqueue_scripts', 'myplugin_enqueue_map' );
function myplugin_enqueue_map() {
wp_enqueue_script(
'myplugin-map',
plugins_url( 'js/map.js', __FILE__ ),
array(),
'2.0.0',
array( 'in_footer' => true )
);
wp_add_inline_script(
'myplugin-map',
'const mypluginMap = ' . wp_json_encode( array( 'apiBase' => esc_url_raw( rest_url( 'myplugin/v1' ) ) ) ) . ';',
'before'
);
}Attaching an 'after' inline script to a script that uses the 'defer' strategy makes WordPress fall back to blocking loading for it; use the 'before' position to keep deferral intact.
Common problems and fixes · 4
- Why doesn't my script show up on the page at all?
- Why did calling wp_enqueue_script() a second time not update my src or dependencies?
- Why does passing true as the fifth argument make my strategy and fetchpriority disappear?
- Why doesn't a handle like 'my-script?ver=2' register the way I expect?
Why doesn't my script show up on the page at all?
Why did calling wp_enqueue_script() a second time not update my src or dependencies?
Why does passing true as the fifth argument make my strategy and fetchpriority disappear?
if ( ! is_array( $args ) ) { $args = array( 'in_footer' => (bool) $args ); }, so any non-array value you pass is collapsed down to just an in_footer flag; 'strategy' and 'fetchpriority' are never set.
- Pass an array instead: array( 'in_footer' => true, 'strategy' => 'defer' ).Why doesn't a handle like 'my-script?ver=2' register the way I expect?
Alternatives and related functions
wp_register_script- When you want to register a script now but decide later, conditionally, whether to actually enqueue it.
wp_deregister_script- When you need to replace or remove a script that was already registered under the same handle before re-adding it.
wp_add_inline_script- When you need to attach a small block of inline JavaScript before or after a script this function already queued.
wp_enqueue_style- When the asset you are loading is a stylesheet rather than a script.
Performance profile
How much work a call to wp_enqueue_script() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Trivial
- Scaling
- Constant
- Instructions
- 19–47
- Plugin surface
- None
- Called by
- 50
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 47.
Nothing here hands control to plugin code.
50 places in core call this, so the cost is paid more often than your own code shows.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_enqueue_script() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
empty($args) | 19 | _wp_scripts_maybe_doing_it_wrong(), wp_scripts(), ->enqueue() |
empty($args) | 27–32 | _wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->enqueue() |
!empty($args) | 34–39 | _wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), _wp_scripts_add_args_data(), ->enqueue() |
empty($args) | 35–40 | _wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add(), ->enqueue() |
!empty($args) | 42–47 | _wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add(), _wp_scripts_add_args_data(), ->enqueue() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 47 instructions, 19–47 executed per call, 5 branches. The work does not change between versions.
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 3
- _wp_scripts_maybe_doing_it_wrong()Helper function to output a _doing_it_wrong message when applicable.
- wp_scripts()Initializes $wp_scripts if it has not been set.
- _wp_scripts_add_args_data()Adds the data for the recognized args and warns for unrecognized args.
Used by · 50
- Custom_Background::admin_load()Sets up the enqueue for the CSS & JavaScript files.
- Custom_Image_Header::js_includes()Sets up the enqueue for the JavaScript files.
- Featured_Content::enqueue_scripts()Enqueues the tag suggestion script.
- Twenty_Fourteen_Ephemera_Widget::enqueue_scripts()Enqueues scripts.
- Twenty_Twenty_One_Customize_Color_Control::enqueue()Enqueues control related scripts/styles.
- Twenty_Twenty_One_Dark_Mode::customize_controls_enqueue_scripts()Enqueues scripts for the customizer.
- Twenty_Twenty_One_Dark_Mode::editor_custom_color_variables()Enqueues editor custom color variables & scripts.
- WP_Admin_Bar::initialize()Initializes the admin bar.
- WP_Block::render()Generates the render output for the block.
- WP_Customize_Color_Control::enqueue()Enqueue scripts/styles for the color picker.
- WP_Customize_Cropped_Image_Control::enqueue()Enqueue control related scripts/styles.
- WP_Customize_Header_Image_Control::enqueue()Enqueues control related scripts/styles.
Show all 50
- WP_Customize_Manager::customize_preview_init()Prints JavaScript settings.
- WP_Customize_Manager::enqueue_control_scripts()Enqueues scripts for customize controls.
- WP_Customize_Nav_Menus::customize_preview_enqueue_deps()Enqueues scripts for the Customizer preview.
- WP_Customize_Nav_Menus::enqueue_scripts()Enqueues scripts and styles for Customizer pane.
- WP_Customize_Selective_Refresh::enqueue_preview_scripts()Enqueues preview scripts.
- WP_Customize_Widgets::customize_preview_enqueue()Enqueues scripts for the Customizer preview.
- WP_Customize_Widgets::enqueue_scripts()Enqueues scripts and styles for Customizer panel and export data to JavaScript.
- WP_Internal_Pointers::enqueue_scripts()Initializes the new feature pointers.
- WP_Privacy_Policy_Content::notice()Adds a notice with a link to the guide when editing the privacy policy page.
- WP_Widget_Custom_HTML::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- WP_Widget_Media::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- WP_Widget_Media_Audio::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Audio::enqueue_preview_scripts()Enqueue preview scripts.
- WP_Widget_Media_Gallery::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Image::enqueue_admin_scripts()Loads the required media files for the media manager and scripts for media widgets.
- WP_Widget_Media_Video::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- WP_Widget_Media_Video::enqueue_preview_scripts()Enqueue preview scripts.
- WP_Widget_Text::enqueue_admin_scripts()Loads the required scripts and styles for the widget control.
- _WP_Editors::enqueue_scripts()
- _wp_get_iframed_editor_assets()Collect the block editor assets that need to be loaded into the editor's iframe.
- add_thickbox()Enqueues the default ThickBox js and css.
- do_accordion_sections()Meta Box Accordion Template Function.
- enqueue_comment_hotkeys_js()Enqueues comment shortcuts jQuery script.
- enqueue_editor_block_styles_assets()Function responsible for enqueuing the assets required for block styles functionality on the editor.
- media_upload_gallery()Retrieves the legacy media uploader form in an iframe.
- register_and_do_post_meta_boxes()Registers the default post meta boxes, and runs the `do_meta_boxes` actions.
- render_block_core_comments()Renders the `core/comments` block on the server.
- render_block_core_post_comments_form()Renders the `core/post-comments-form` block on the server.
- the_block_editor_meta_boxes()Renders the meta boxes forms.
- the_custom_header_markup()Prints the markup for a custom header.
- twenty_twenty_one_scripts()Enqueues scripts and styles.
- twentyeleven_admin_enqueue_scripts()Enqueues styles and scripts for the theme options page.
- twentyeleven_customize_preview_js()Binds JS handlers to make Customizer preview reload changes asynchronously.
- twentyfifteen_customize_control_js()Binds JS listener to make Customizer color_scheme control.
- twentyfifteen_customize_preview_js()Binds JS handlers to make the Customizer preview reload changes asynchronously.
- twentyfifteen_scripts()Enqueues scripts and styles.
- twentyfourteen_customize_preview_js()Binds JS handlers to make Customizer preview reload changes asynchronously.
- twentyfourteen_scripts()Enqueues scripts and styles for the front end.
Source code
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $args = array() ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); $wp_scripts = wp_scripts(); if ( $src || ! empty( $args ) ) { /** @var array{ 0: non-empty-string, 1?: string } $_handle */ $_handle = explode( '?', $handle ); if ( ! is_array( $args ) ) { $args = array( 'in_footer' => (bool) $args, ); } if ( $src ) { $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } if ( ! empty( $args ) ) { _wp_scripts_add_args_data( $wp_scripts, $_handle[0], $args ); } } $wp_scripts->enqueue( $handle );}Changelog
Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.0.4 tag, from
src/wp-includes/functions.wp-scripts.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.