wppaste
WordPress

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
Source
wp-includes/functions.wp-scripts.php:359

Queues a script handle for output, registering it with $src, $deps, $ver, and $args first if that handle hasn't already been registered. Registration only happens when $src is non-empty, and it never overwrites a handle registered earlier by core or another plugin. Since 6.3.0, $args can carry a 'strategy' of defer or async in addition to the older in_footer boolean, so check which shape your target version expects before passing it.

Enqueues a script.

Description

Registers the script if $src provided (does NOT overwrite), and enqueues it.

Compatibility

WordPress
since 6.3.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 additional script loading strategies. 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()
  • $strategystring

    Optional. If provided, may be either 'defer' or 'async'.
  • $in_footerbooldefault: 'false'

    Optional. Whether to print the script in the footer.

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 script with a defer or async loading strategy

Register and queue a script that uses the strategy option added to the $args array in WordPress 6.3.0.

wp_enqueue_script(
	'wppaste-example-async',
	'https://example.com/assets/example.js',
	array(),
	'2.0.1',
	array(
		'strategy'  => 'async',
		'in_footer' => true,
	)
);

$wp_scripts = wp_scripts();
$dependency = $wp_scripts->registered['wppaste-example-async'];

echo 'Registered src: ' . esc_html( $dependency->src ) . "\n";
echo 'Loading strategy: ' . esc_html( $dependency->extra['strategy'] ) . "\n";
echo 'Placed in footer group: ' . ( ! empty( $dependency->extra['group'] ) ? 'yes' : 'no' ) . "\n";
echo 'Enqueued: ' . ( wp_script_is( 'wppaste-example-async', 'enqueued' ) ? 'yes' : 'no' );

The script's actual src file does not need to exist for registration and enqueueing to succeed; the browser only 404s on the asset itself.

Only enqueue a script on single post pages

Hook the enqueue call to wp_enqueue_scripts so it only fires for singular posts, then confirm it ran from the footer.

add_action( 'wp_enqueue_scripts', function() {
	if ( is_singular( 'post' ) ) {
		wp_enqueue_script(
			'theme-post-highlighter',
			get_theme_file_uri( 'assets/post-highlighter.js' ),
			array( 'jquery' ),
			'1.0.0',
			array(
				'strategy'  => 'defer',
				'in_footer' => true,
			)
		);
	}
} );

add_action( 'wp_footer', function() {
	echo '<p>' . esc_html( wp_script_is( 'theme-post-highlighter', 'enqueued' ) ? 'Highlighter script queued for this post.' : 'Highlighter script not queued here.' ) . '</p>';
} );

Load the front end permalink for post ID 1 to see the footer message; visiting any other template shows the 'not queued' message instead.

Common problems and fixes · 3

Why did calling wp_enqueue_script with a new $src on an already-registered handle not change the script's source?

Registration only happens inside this function when $src is truthy, and the underlying WP_Dependencies::add() call it delegates to refuses to overwrite a handle that's already registered under that name. If core, a plugin, or an earlier call already registered that handle, your new $src is silently ignored.

Why am I seeing a 'doing it wrong' notice when I call wp_enqueue_script?

The function opens by calling _wp_scripts_maybe_doing_it_wrong(), which flags calls made before the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts action has fired for the current request.

Why did passing an $args array not put my script in the footer?

The footer placement only happens when $args is an array containing a truthy 'in_footer' key; the source checks !empty( $args['in_footer'] ) before calling add_data( ..., 'group', 1 ). Passing an array without that key, or passing a strategy-only array, leaves the script in the default head position.

Alternatives and related functions

wp_register_script
When you want to make a script handle available for other code to depend on without printing it yet, and enqueue it separately once it's actually needed on the page.
wp_deregister_script
When you need to replace a script that's already been registered under the same handle, since wp_enqueue_script itself will not overwrite an existing registration.
wp_script_is
When you need to check whether a handle is already registered, enqueued, or done before deciding whether to call wp_enqueue_script again.
wp_add_inline_script
When you need to attach inline JavaScript immediately before or after the script handle that wp_enqueue_script queues.

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

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
19–58

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 58.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
50

50 places in core call this, so the cost is paid more often than your own code shows.

What one call costs · 7 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.

WhenInstructionsCalls it makes
empty($args)19_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), ->enqueue()
empty($args)29–34_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->enqueue()
always36–43_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add_data(), ->enqueue()
empty($args)37–42_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add(), ->enqueue()
always44–51_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add(), ->add_data(), ->enqueue()
!empty($args)45–50_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add_data(), ->add_data(), ->enqueue()
!empty($args)53–58_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), explode(), ->add(), ->add_data(), ->add_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: 58 instructions, 19–58 executed per call, 6 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 · 2

Used by · 50

Show all 50

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 ) ) {		$_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['in_footer'] ) ) {			$wp_scripts->add_data( $_handle[0], 'group', 1 );		}		if ( ! empty( $args['strategy'] ) ) {			$wp_scripts->add_data( $_handle[0], 'strategy', $args['strategy'] );		}	} 	$wp_scripts->enqueue( $handle );}

Changelog

Introduced in 2.1.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.3.0
The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.from the docblock
2.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.