wppaste
WordPress

wp_register_script( string $handle, string|false $src, string[] $deps = array(), string|bool|null $ver = false, array|bool $args = array() ): bool

Since
2.1.0, 4.3.0, 6.3.0, 6.9.0, 7.0.0
Source
wp-includes/functions.wp-scripts.php:278
Registers a new script.

Description

Registers a script to be enqueued later using the wp_enqueue_script() function.

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.
$srcstring|false
Full URL of the script, or path of the script relative to the WordPress root directory.
If source is set to false, script is an alias of other scripts it depends on.
$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()
  • $strategystring

    Optional. 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_dependenciesarray

    Optional. IDs for module dependencies loaded via dynamic import. Default empty array. For the full data format, see the $deps param of wp_register_script_module(). When provided, the script must either be printed in the footer (with in_footer set to true) or use a deferred loading strategy (defer), so that the script modules import map is printed before the script is evaluated. Otherwise dynamic imports may fail to resolve.

Return value

bool
Whether the script has been registered. True on success, false on failure.

Performance profile

How much work a call to wp_register_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
27–30

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
10

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

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_register_script() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always27–30_wp_scripts_maybe_doing_it_wrong(), wp_scripts(), ->add(), _wp_scripts_add_args_data()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 30 instructions, 27–30 executed per call, 1 branch. 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

Used by · 10

Source code

function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args = array() ) {	if ( ! is_array( $args ) ) {		$args = array(			'in_footer' => (bool) $args,		);	}	_wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); 	$wp_scripts = wp_scripts(); 	$registered = $wp_scripts->add( $handle, $src, $deps, $ver );	_wp_scripts_add_args_data( $wp_scripts, $handle, $args ); 	return $registered;}

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.

7.0.0
The $module_dependencies parameter of type string[] was added to the $args parameter of type array.from the docblock
6.9.0
The $fetchpriority parameter of type string was added to the $args parameter of type array.from the docblock
6.3.0
The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.from the docblock
4.3.0
A return value was added.from the docblock
2.1.0
Introduced.from the docblock

About this page

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