wp_register_script()WP 2.1.0

Registers a script file to later enqueue it using the function wp_enqueue_script().

You need to register a script separately in cases where you do not know in advance whether you will use it on the page or not, but you are sure that it will be useful on some pages. So, to avoid specifying all registration parameters for each page where the script will be needed, you can register it in one place and then simply enqueue it by its identifier where needed.

When writing a plugin or theme, it is recommended to enqueue scripts through this function or its equivalent wp_enqueue_script().

The function should be called on the following hook actions because calling the function earlier than these events may cause issues:

Default WP scripts are registered on the init event. Delete the function wp_default_scripts() and the hook wp_default_scripts.

To register a script that already exists in WP, you first need to deregister it using the function wp_deregister_script(), and then register it again with the required parameters.

No Hooks.

Returns

true|false. 3 returns true/false. Previously returned nothing.

Usage

wp_register_script( $handle, $src, $deps, $ver, $in_footer );
$handle(string) (required)
The name of the script. Must be unique, as it will be used for calling later in the wp_enqueue_script() function.
$src(string) (required)
URL, path to the script, for example, http://example.com/wp-content/themes/my-theme/my-theme-script.js. Never write the URL directly if it is located in engine files or a plugin, use special path functions: plugins_url() for plugins and get_template_directory_uri() for themes. Remote scripts should be specified without a protocol, like this: //otherdomain.com/js/their-script.js.
$deps(array)
An array of names of all registered scripts that this one depends on. The scripts specified here will be loaded before the current one. Specify false if there are no dependent scripts.
Default: array()
$ver(string|false|null)

The version of the script that will be added to the end of the file path as an argument (?ver=1.1). This parameter is needed to ensure that the correct version of the script is loaded by users, bypassing the cache.

  • When false WordPress will add the current WP version at the end.
  • When null, no version will be added.

Default: false

$args(array)

Parameters for enqueuing the script. Two parameters are supported:

Before WP 6.3 this parameter was called $in_footer and accepted true/false.
Starting from version 6.3, the parameter began to accept an array of data, and the previous parameter $in_footer was moved to the array element in_footer:

[
	'in_footer' => true,
	'strategy'  => 'async',
	]

For more details about this change read here.

Default: []

  • in_footer(bool) - Default: false. Whether to output the script in the footer. By default, scripts are placed in the head section. If this parameter is true, the script will be output at the end of the body tag.

    If due to dependencies on other scripts it is not possible to enqueue the current script in the footer, the value of this variable will be ignored and the script will be enqueued in the head.

    The function wp_footer() should be called in the theme before the closing </body> tag.

    Behaves the same as the previous (before WP 6.3) implementation of the $in_footer parameter.

  • strategy(string) - Default: ''. Indicates how (by what strategy) the script should be loaded. Two values are available: defer and async for deferred and asynchronous scripts, respectively.

    By default, blocking behavior is used, which maintains backward compatibility with WP versions below 6.3.

    Use wp_register_script_module() to register the script as a module.

Examples

3

#1 How to connect jquery from Google

Read in separate article.

2

#2 Connecting an external script (the easy way)

add_action( 'init', 'register_remote_scripts' );
add_action( 'wp_enqueue_scripts', 'add_remote_scripts' );

function register_remote_scripts(){

	wp_register_script( 'someScript-js', 'https://domain.com/someScript.js' , '', '', true );
}

function add_remote_scripts(){

	wp_enqueue_script( 'someScript-js' );
}

Or just:

add_action( 'wp_enqueue_scripts', 'add_remote_scripts' );

function add_remote_scripts(){

	wp_enqueue_script( 'someScript-js', 'https://domain.com/someScript.js' , '', '', true );
}
1

#3 Depend one script on another

In a case where first script should be loaded only if another second script is loaded:

add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );

function my_enqueue_scripts(){

	wp_register_script( 'first', get_template_directory_uri() . 'js/first.js' );

	wp_enqueue_script( 'second', get_template_directory_uri() . 'js/second.js', array( 'first' ) );
}

Here, first.js will be loaded before second.js only if second.js is enqueued/loaded.

0

#4 Separate registration of the script and its connection

add_action( 'init', 'register_myscripts' );
add_action( 'wp_enqueue_scripts', 'add_myscripts' );

//register the script
function register_myscripts(){

	$url = get_template_directory_uri() .'/myscript.js';

	wp_register_script( 'myfirstscript', $url, [ 'jquery', 'jquery-ui' ], 'v2.0', true );
}

// connect the scripts (queue the output to html)
function add_myscripts() {

	wp_enqueue_script( 'myfirstscript' );
}
0

#5 Register and connect immediately

If we know beforehand that we need the script only on one page, we can register it immediately and connect it to the output with wp_enqueue_script():

add_action( 'wp_enqueue_scripts', 'my_scripts_method' );

function my_scripts_method() {

	$script_url = plugins_url( '/js/newscript.js', __FILE__ );

	wp_enqueue_script( 'custom-script', $script_url, ['jquery'], '1.0', true );
}

For more examples, see the wp_enqueue_script() function description.

0

#6 defer and async script registration

Example: deferred (defer) script inclusion in the header

The loading strategy is specified by passing a key-value pair of the strategy in the $args parameter (formerly $in_footer).

wp_register_script(
	'my_script',
	'https://example.com/path/to/my_script.js',
	[],
	'1.0',
	[
		'strategy' => 'defer'
	]
);
Example: asynchronous (async) script inclusion in the footer
wp_register_script(
	'my_script',
	'https://example.com/path/to/my_script.js',
	[],
	'1.0',
	[
		'in_footer' => true,
		'strategy'  => 'async',
	]
);

Scripts registered by WordPress

WordPress registers many popular scripts by default (out of the box), i.e., such a script exists by default in WP. To add such a script to a page, you need to use its identifier in wp_enqueue_script( $id ).

The list was compiled from version WP 5.2.2

The current list can be viewed in the code of the function wp_default_scripts().

Name ID Dependency
  utils  
WP Common common jquery, hoverIntent, utils
  wp-a11y jquery
Simple AJAX Code-Kit sack  
QuickTags quicktags  
ColorPicker (deprecated) colorpicker prototype
  editor utils, jquery
clipboard.js clipboard  
  wp-fullscreen-stub  
WordPress AJAX Response wp-ajax-response jquery
  wp-api-request jquery
  wp-pointer jquery-ui-widget, jquery-ui-position
Autosave autosave heartbeat
Heartbeat heartbeat jquery, wp-hooks
  wp-auth-check heartbeat
List Manipulation wp-lists wp-ajax-response, jquery-color
  prototype  
  scriptaculous-root prototype
  scriptaculous-builder scriptaculous-root
  scriptaculous-dragdrop scriptaculous-builder, scriptaculous-effects
  scriptaculous-effects scriptaculous-root
  scriptaculous-slider scriptaculous-effects
  scriptaculous-sound scriptaculous-root
  scriptaculous-controls scriptaculous-root
  scriptaculous scriptaculous-dragdrop, scriptaculous-slider, scriptaculous-controls
  cropper scriptaculous-dragdrop
jQuery jquery jquery-core, jquery-migrate
  jquery-core  
  jquery-migrate  
jQuery UI Core jquery-ui-core jquery
jQuery UI Effects jquery-effects-core jquery
jQuery UI Effects - Blind jquery-effects-blind jquery-effects-core
jQuery UI Effects - Bounce jquery-effects-bounce jquery-effects-core
jQuery UI Effects - Clip jquery-effects-clip jquery-effects-core
jQuery UI Effects - Drop jquery-effects-drop jquery-effects-core
jQuery UI Effects - Explode jquery-effects-explode jquery-effects-core
jQuery UI Effects - Fade jquery-effects-fade jquery-effects-core
jQuery UI Effects - Fold jquery-effects-fold jquery-effects-core
jQuery UI Effects - Highlight jquery-effects-highlight jquery-effects-core
  jquery-effects-puff jquery-effects-core, jquery-effects-scale
jQuery UI Effects - Pulsate jquery-effects-pulsate jquery-effects-core
jQuery UI Effects - Scale jquery-effects-scale jquery-effects-core, jquery-effects-size
jQuery UI Effects - Shake jquery-effects-shake jquery-effects-core
  jquery-effects-size jquery-effects-core
jQuery UI Effects - Slide jquery-effects-slide jquery-effects-core
jQuery UI Effects - Transfer jquery-effects-transfer jquery-effects-core
jQuery UI Accordion jquery-ui-accordion jquery-ui-core, jquery-ui-widget
jQuery UI Autocomplete jquery-ui-autocomplete jquery-ui-menu, wp-a11y
jQuery UI Button jquery-ui-button jquery-ui-core, jquery-ui-widget
jQuery UI Datepicker jquery-ui-datepicker jquery-ui-core
jQuery UI Dialog jquery-ui-dialog jquery-ui-resizable, jquery-ui-draggable, jquery-ui-button, jquery-ui-position
jQuery UI Draggable jquery-ui-draggable jquery-ui-mouse
jQuery UI Droppable jquery-ui-droppable jquery-ui-draggable
jQuery UI Menu jquery-ui-menu jquery-ui-core, jquery-ui-widget, jquery-ui-position
jQuery UI Mouse jquery-ui-mouse jquery-ui-core, jquery-ui-widget
jQuery UI Position jquery-ui-position jquery
jQuery UI Progressbar jquery-ui-progressbar jquery-ui-core, jquery-ui-widget
jQuery UI Resizable jquery-ui-resizable jquery-ui-mouse
jQuery UI Selectable jquery-ui-selectable jquery-ui-mouse
jQuery UI Selectmenu jquery-ui-selectmenu jquery-ui-menu
jQuery UI Slider jquery-ui-slider jquery-ui-mouse
jQuery UI Sortable jquery-ui-sortable jquery-ui-mouse
jQuery UI Spinner jquery-ui-spinner jquery-ui-button
jQuery UI Tabs jquery-ui-tabs jquery-ui-core, jquery-ui-widget
jQuery UI Tooltips jquery-ui-tooltip jquery-ui-core, jquery-ui-widget, jquery-ui-position
jQuery UI Widget jquery-ui-widget jquery
jQuery Form jquery-form jquery
jQuery Color jquery-color jquery
jQuery Schedule schedule jquery
  jquery-query jquery
  jquery-serialize-object jquery
jQuery Hotkeys jquery-hotkeys jquery
  jquery-table-hotkeys jquery, jquery-hotkeys
  jquery-touch-punch jquery-ui-widget, jquery-ui-mouse
jQuery Suggest suggest jquery
  imagesloaded  
Masonry (native Javascript) masonry imagesloaded
jQuery Masonry jquery-masonry jquery, masonry
ThickBox thickbox jquery
Jcrop jcrop jquery
SWFObject swfobject  
  moxiejs  
Plupload Core plupload moxiejs
Plupload All Runtimes plupload-all plupload
Plupload HTML5 plupload-html5 plupload
Plupload Flash plupload-flash plupload
Plupload Silverlight plupload-silverlight plupload
Plupload HTML4 plupload-html4 plupload
  plupload-handlers plupload, jquery
  wp-plupload plupload, jquery, json2, media-models
SWFUpload swfupload  
  swfupload-all swfupload
SWFUpload Handlers swfupload-handlers swfupload-all, jquery
Threaded Comments comment-reply  
JSON for JS json2  
Underscore js underscore  
Backbone js backbone underscore, jquery
  wp-util underscore, jquery
  wp-sanitize jquery
  wp-backbone backbone, wp-util
  revisions wp-backbone, jquery-ui-slider, hoverIntent
  imgareaselect jquery
  mediaelement jquery, mediaelement-core, mediaelement-migrate
  mediaelement-core  
  mediaelement-migrate  
  mediaelement-vimeo mediaelement
MediaElement.js (WP 3.6+) wp-mediaelement mediaelement
  wp-codemirror  
  csslint  
  esprima  
  jshint esprima
  jsonlint  
  htmlhint  
  htmlhint-kses htmlhint
  code-editor jquery, wp-codemirror, underscore
  wp-theme-plugin-editor wp-util, wp-sanitize, jquery, jquery-ui-core, wp-a11y, underscore
  wp-playlist wp-util, backbone, mediaelement
  zxcvbn-async  
Password Strength Meter password-strength-meter jquery, zxcvbn-async
  user-profile jquery, password-strength-meter, wp-util
  language-chooser jquery
  user-suggest jquery-ui-autocomplete
  admin-bar  
  wplink jquery, wp-a11y
  wpdialogs jquery-ui-dialog
Word Count word-count  
Media Upload media-upload thickbox, shortcode
jQuery HoverIntent hoverIntent jquery
  customize-base jquery, json2, underscore
  customize-loader customize-base
  customize-preview wp-a11y, customize-base
  customize-models underscore, backbone
  customize-views jquery, underscore, imgareaselect, customize-models, media-editor, media-views
  customize-controls customize-base, wp-a11y, wp-util, jquery-ui-core
  customize-selective-refresh jquery, wp-util, customize-preview
  customize-widgets jquery, jquery-ui-sortable, jquery-ui-droppable, wp-backbone, customize-controls
  customize-preview-widgets jquery, wp-util, customize-preview, customize-selective-refresh
  customize-nav-menus jquery, wp-backbone, customize-controls, accordion, nav-menu
  customize-preview-nav-menus jquery, wp-util, customize-preview, customize-selective-refresh
  wp-custom-header wp-a11y
  accordion jquery
  shortcode underscore
  media-models wp-backbone
  wp-embed  
  media-views utils, media-models, wp-plupload, jquery-ui-sortable, wp-mediaelement, wp-api-request
  media-editor shortcode, media-views
  media-audiovideo media-editor
  mce-view shortcode, jquery, media-views, media-audiovideo
  wp-api jquery, backbone, underscore, wp-api-request
  react wp-polyfill
  react-dom react
  moment  
  lodash  
  wp-polyfill-fetch  
  wp-polyfill-formdata  
  wp-polyfill-node-contains  
  wp-polyfill-element-closest  
  wp-polyfill  
  wp-tinymce-root  
  wp-tinymce wp-tinymce-root
  wp-tinymce-lists wp-tinymce
  wp-api-fetch wp-polyfill, wp-i18n, wp-url, wp-hooks
  wp-annotations wp-data, wp-hooks, wp-i18n, wp-polyfill, wp-rich-text
  wp-autop wp-polyfill
  wp-blob wp-polyfill
  wp-blocks wp-autop, wp-blob, wp-block-serialization-default-parser, wp-data, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-polyfill, wp-shortcode, lodash
  wp-block-library editor, lodash, wp-api-fetch, wp-autop, wp-blob, wp-block-editor, wp-blocks, wp-components, wp-compose, wp-core-data, wp-data, wp-date, wp-editor, wp-element, wp-html-entities, wp-i18n, wp-keycodes, wp-polyfill, wp-url, wp-viewport, wp-rich-text
  wp-block-serialization-default-parser  
  wp-block-editor lodash, wp-a11y, wp-blob, wp-blocks, wp-components, wp-compose, wp-core-data, wp-data, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-rich-text, wp-token-list, wp-url, wp-viewport, wp-wordcount
  wp-components lodash, moment, wp-a11y, wp-api-fetch, wp-compose, wp-dom, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-is-shallow-equal, wp-keycodes, wp-polyfill, wp-rich-text, wp-url
  wp-compose lodash, wp-element, wp-is-shallow-equal, wp-polyfill
  wp-core-data lodash, wp-api-fetch, wp-data, wp-deprecated, wp-polyfill, wp-url
  wp-data lodash, wp-compose, wp-element, wp-is-shallow-equal, wp-polyfill, wp-priority-queue, wp-redux-routine
  wp-date moment, wp-polyfill
  wp-deprecated wp-polyfill, wp-hooks
  wp-dom lodash, wp-polyfill
  wp-dom-ready wp-polyfill
  wp-edit-post jquery, lodash, postbox, media-models, media-views, wp-a11y, wp-api-fetch, wp-block-editor, wp-block-library, wp-blocks, wp-components, wp-compose, wp-core-data, wp-data, wp-dom-ready, wp-editor, wp-element, wp-embed, wp-i18n, wp-keycodes, wp-notices, wp-nux, wp-plugins, wp-polyfill, wp-url, wp-viewport
  wp-editor lodash, wp-api-fetch, wp-blob, wp-block-editor, wp-blocks, wp-components, wp-compose, wp-core-data, wp-data, wp-date, wp-deprecated, wp-element, wp-hooks, wp-html-entities, wp-i18n, wp-keycodes, wp-notices, wp-nux, wp-polyfill, wp-url, wp-viewport, wp-wordcount
  wp-element wp-polyfill, react, react-dom, lodash, wp-escape-html
  wp-escape-html wp-polyfill
  wp-format-library wp-block-editor, wp-components, wp-editor, wp-element, wp-i18n, wp-keycodes, wp-polyfill, wp-rich-text, wp-url
  wp-hooks wp-polyfill
  wp-html-entities wp-polyfill
  wp-i18n wp-polyfill
  wp-is-shallow-equal wp-polyfill
  wp-keycodes lodash, wp-polyfill, wp-i18n
  wp-list-reusable-blocks lodash, wp-api-fetch, wp-components, wp-compose, wp-element, wp-i18n, wp-polyfill
  wp-notices lodash, wp-a11y, wp-data, wp-polyfill
  wp-nux wp-element, lodash, wp-components, wp-compose, wp-data, wp-i18n, wp-polyfill, lodash
  wp-plugins lodash, wp-compose, wp-element, wp-hooks, wp-polyfill
  wp-priority-queue  
  wp-redux-routine wp-polyfill
  wp-rich-text lodash, wp-data, wp-escape-html, wp-polyfill
  wp-shortcode wp-polyfill, lodash
  wp-token-list lodash, wp-polyfill
  wp-url wp-polyfill
  wp-viewport wp-polyfill, wp-element, wp-data, wp-compose, lodash
  wp-wordcount wp-polyfill

The list is obtained from the global variable $GLOBALS['wp_scripts']. Registered scripts may change depending on the page you are on. In the admin area, the list will be larger.

Notes

Changelog

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

wp_register_script() code WP 7.0

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;
}