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
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()$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.
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
- Scaling
- Constant
- Instructions
- 27–30
- Plugin surface
- None
- Called by
- 10
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 30.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 27–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
- _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 · 10
- enqueue_editor_block_styles_assets()Function responsible for enqueuing the assets required for block styles functionality on the editor.
- register_block_script_handle()Finds a script handle for the selected block metadata field.
- twenty_twenty_one_scripts()Enqueues scripts and styles.
- twentyfifteen_scripts()Enqueues scripts and styles.
- twentyseventeen_scripts()Enqueues scripts and styles.
- twentysixteen_scripts()Enqueues scripts and styles.
- wp_font_library_render_page()Render the font-library page.
- wp_font_library_wp_admin_enqueue_scripts()Enqueue scripts and styles for the font-library-wp-admin page.
- wp_options_connectors_render_page()Render the options-connectors page.
- wp_options_connectors_wp_admin_enqueue_scripts()Enqueue scripts and styles for the options-connectors-wp-admin page.
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.
Signature, return type and hooks compared across 5 parsed releases.
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.