wp_enqueue_script() – Function | Developer.WordPress.org
Enqueues a script.
Description
Registers the script if $src provided (does NOT overwrite), and enqueues it.
See also
Parameters
$handlestringrequiredName of the script. Should be unique.
$srcstringrequiredFull URL of the script, or path of the script relative to the WordPress root directory.
Default empty.$depsstring[]optionalAn array of registered script handles this script depends on.
Default:
array()$verstring|bool|nulloptionalString 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|booloptionalAn array of additional script loading strategies.
Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
strategystringOptional. If provided, may be either
'defer'or'async'.in_footerboolOptional. Whether to print the script in the footer. Default
'false'.fetchprioritystringOptional. The fetch priority for the script. Default
'auto'.
Default:
array()
More Information
Usage
wp_enqueue_script( $handle, $src, $deps, $ver, $args );
Links a script file to the generated page at the right time according to the script dependencies, if the script has not been already included and if all the dependencies have been registered. You could either link a script with a handle previously registered using the wp_register_script() function, or provide this function with all the parameters necessary to link a script.
This is the recommended method of linking JavaScript to a WordPress generated page.
As of WordPress 6.3, the new $args parameter – that replaces/overloads the prior $in_footer parameter – can be used to specify a script loading strategy. See the sections to follow for more information.
Supported strategies are as follows:
- defer
- Added by specifying an array key value pair of
'strategy' => 'defer'to the$argsparameter. - Scripts marked for deferred execution — via the
deferscript attribute — are only executed once the DOM tree has fully loaded (but before theDOMContentLoadedand window load events). Deferred scripts are executed in the same order they were printed/added in the DOM, unlike asynchronous scripts.
- Added by specifying an array key value pair of
- async
- Added by specifying an array key value pair of
'strategy' => 'async'to the$argsparameter. - Scripts marked for asynchronous execution — via the
asyncscript attribute — are executed as soon as they are loaded by the browser. Asynchronous scripts do not have a guaranteed execution order, as script B (although added to the DOM after script A) may execute first given that it may complete loading prior to script A. Such scripts may execute either before the DOM has been fully constructed or after theDOMContentLoadedevent.
- Added by specifying an array key value pair of
Following is an example of specifying a loading strategy during script enqueuing:
wp_enqueue_script(
'foo',
'/path/to/foo.js',
array(),
'1.0.0',
array(
'strategy' => 'defer',
)
);
The same approach applies when using
wp_register_script().
When applying a loading strategy via either the wp_register_script() and wp_enqueue_script() functions, the scripts dependency tree is taken into consideration and the most eligible loading strategy is applied.
While the intended (delayed) strategy passed by the code author may not be the final one, it will never be a stricter one, thus maintaining the integrity of the dependency tree.
Notes
- The function should be called using the wp_enqueue_scripts action hook if you want to call it on the front-end of the site, like in the examples above. To call it on the administration screens, use the admin_enqueue_scripts action hook. For the login screen, use the login_enqueue_scripts action hook. Calling it outside of an action hook can lead to problems, see the ticket #11526 for details.
- If you try to register or enqueue an already registered handle with different parameters, the new parameters will be ignored. Instead, use wp_deregister_script() and register the script again with the new parameters.
- jQuery UI Effects is not included with the jquery-ui-core handle.
- This function relies on the use of wp_head() and wp_footer() by the active theme. This means that it may not work with a few very old themes that do not call these functions. This is useful to keep in mind when debugging ancient themes.
- Uses: WP_Scripts::add(), WP_Scripts::add_data() and WP_Scripts::enqueue().
- Uses global: (unknown type) $wp_scripts.
Default Scripts and JS Libraries Included and Registered by WordPress
By default, WordPress installation includes many popular javascript libraries and scripts commonly used by web developers besides the scripts used by WordPress itself. Some of them are listed in the table below.
For a detailed list of names that can be used in place of the $handle parameter, see wp_register_script().
| Script Name | Handle | Needed Dependency * | Script version | License |
|---|---|---|---|---|
| Image Cropper | Image cropper (not used in core, see jcrop) | |||
| Jcrop | jcrop | 0.9.12 | MIT | |
| SWFObject | swfobject | 2.2-20120417 | MIT | |
| SWFUpload | swfupload | 2201-20110113 | MIT | |
| SWFUpload Degrade | swfupload-degrade | 2201 | MIT | |
| SWFUpload Queue | swfupload-queue | 2201 | MIT | |
| SWFUpload Handlers | swfupload-handlers | 2201-20110524 | MIT | |
| jQuery | jquery | json2 (for AJAX calls) | 3.6.0 | MIT + (MIT OR BSD) |
| jQuery Form | jquery-form | jquery | 4.3.0 | MIT OR LGPLv3 |
| jQuery Color | jquery-color | jquery | 2.2.0 | MIT+CC0 + (MIT OR GPLv2) |
| jQuery Masonry | jquery-masonry | jquery | 3.1.2b | MIT |
| Masonry (native Javascript) | masonry | imagesloaded | 4.2.2 | MIT |
| jQuery UI Core | jquery-ui-core | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Widget | Now part of `jquery-ui-core` | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Accordion | jquery-ui-accordion | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Autocomplete | jquery-ui-autocomplete | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Button | jquery-ui-button | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Datepicker | jquery-ui-datepicker | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Dialog | jquery-ui-dialog | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Draggable | jquery-ui-draggable | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Droppable | jquery-ui-droppable | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Menu | jquery-ui-menu | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Mouse | jquery-ui-mouse | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Position | Now part of `jquery-ui-core` | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Progressbar | jquery-ui-progressbar | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Selectable | jquery-ui-selectable | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Resizable | jquery-ui-resizable | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Selectmenu | jquery-ui-selectmenu | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Sortable | jquery-ui-sortable | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Slider | jquery-ui-slider | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Spinner | jquery-ui-spinner | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Tooltips | jquery-ui-tooltip | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Tabs | jquery-ui-tabs | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects | jquery-effects-core | jquery | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Blind | jquery-effects-blind | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Bounce | jquery-effects-bounce | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Clip | jquery-effects-clip | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Drop | jquery-effects-drop | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Explode | jquery-effects-explode | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Fade | jquery-effects-fade | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Fold | jquery-effects-fold | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Highlight | jquery-effects-highlight | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Pulsate | jquery-effects-pulsate | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Scale | jquery-effects-scale | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Shake | jquery-effects-shake | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Slide | jquery-effects-slide | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| jQuery UI Effects – Transfer | jquery-effects-transfer | jquery-effects-core | 1.13.1 | MIT + CC0 + (MIT OR GPLv2) |
| MediaElement.js (WP 3.6+) | wp-mediaelement | jquery | 4.2.16 | MIT |
| jQuery Schedule | schedule | jquery | 20m/1.0.1 | MIT |
| jQuery Suggest | suggest | jquery | 1.1-20110113 | Public domain |
| ThickBox | thickbox | 3.1-20121105 | MIT OR GPLv3 | |
| jQuery HoverIntent | hoverIntent | jquery | 1.10.1 | MIT |
| jQuery Hotkeys | jquery-hotkeys | jquery | 0.2.0 | MIT OR GPLv2 |
| Simple AJAX Code-Kit | sack | 1.6.1 | X11 License | |
| QuickTags | quicktags | 1.3 | LGPL2.1 | |
| Iris (Colour picker) | iris | jquery | 1.1.1 | GPLv2 |
| Farbtastic (deprecated) | farbtastic | jquery | 1.2 | GPLv3 |
| ColorPicker (deprecated) | colorpicker | jquery | v2 | Author’s own copyright |
| Tiny MCE | wp-tinymce | 4.9.4 | LGPL2.1 | |
| Autosave | autosave | |||
| WordPress AJAX Response | wp-ajax-response | |||
| List Manipulation | wp-lists | |||
| WP Common | common | |||
| WP Editor | editorremov | |||
| WP Editor Functions | editor-functions | |||
| AJAX Cat | ajaxcat | |||
| Admin Categories | admin-categories | |||
| Admin Tags | admin-tags | |||
| Admin custom fields | admin-custom-fields | |||
| Password Strength Meter | password-strength-meter | |||
| Admin Comments | admin-comments | |||
| Admin Users | admin-users | |||
| Admin Forms | admin-forms | |||
| XFN | xfn | |||
| Upload | upload | |||
| PostBox | postbox | |||
| Slug | slug | |||
| Post | post | |||
| Page | page | |||
| Link | link | |||
| Comment | comment | |||
| Threaded Comments | comment-reply | |||
| Admin Gallery | admin-gallery | |||
| Media Upload | media-upload | |||
| Admin widgets | admin-widgets | |||
| Word Count | word-count | |||
| Theme Preview | theme-preview | |||
| JSON for JS | json2 | 2015-05-03 | Public domain | |
| Plupload Core | plupload | 2.1.9 | GPLv2 | |
| Plupload All Runtimes | plupload-all | 2.1.1 | GPLv2 | |
| Plupload HTML4 | plupload-html4 | 2.1.1 | GPLv2 | |
| Plupload HTML5 | plupload-html5 | 2.1.1 | GPLv2 | |
| Plupload Flash | plupload-flash | 2.1.1 | GPLv2 | |
| Plupload Silverlight | plupload-silverlight | 2.1.1 | GPLv2 | |
| Underscore js | underscore | 1.13.1 | MIT | |
| Backbone js | backbone | jquery, underscore | 1.4.0 | MIT |
| imagesLoaded | imagesloaded | 4.1.4 | MIT | |
| CodeMirror | wp-codemirror | 5.29.1-alpha-ee20357 | MIT | |
| imgAreaSelect | imgareaselect | jquery | 0.9.8 | MIT AND GPL |
| Removed from Core | |||
|---|---|---|---|
| Script Name | Handle | Removed Version | Replaced With |
| Scriptaculous Root | scriptaculous-root | WP 3.5 | Google Version |
| Scriptaculous Builder | scriptaculous-builder | WP 3.5 | Google Version |
| Scriptaculous Drag & Drop | scriptaculous-dragdrop | WP 3.5 | Google Version |
| Scriptaculous Effects | scriptaculous-effects | WP 3.5 | Google Version |
| Scriptaculous Slider | scriptaculous-slider | WP 3.5 | Google Version |
| Scriptaculous Sound | scriptaculous-sound | WP 3.5 | Google Version |
| Scriptaculous Controls | scriptaculous-controls | WP 3.5 | Google Version |
| Scriptaculous | scriptaculous | WP 3.5 | Google Version |
| Prototype Framework | prototype | WP 3.5 | Google Version |
The list is far from complete. For a complete list of registered files inspect $GLOBALS['wp_scripts'] in the admin UI. Registered scripts might change per requested page.
* The listed dependencies are not complete.
Source
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'] );
}
if ( ! empty( $args['fetchpriority'] ) ) {
$wp_scripts->add_data( $_handle[0], 'fetchpriority', $args['fetchpriority'] );
}
}
$wp_scripts->enqueue( $handle );
}