Enqueues a script.
Registers the script if $src provided (does NOT overwrite), and enqueues it.
$handlestringrequired
$srcstringoptional
Default:''
$depsstring[]optional
Default:array()
$verstring|bool|nulloptional
Default:false
$argsarray|booloptional
strategy string'defer' or 'async'.in_footer bool'false'.Default:array()
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:
'strategy' => 'defer' to the $args parameter.defer script attribute — are only executed once the DOM tree has fully loaded (but before the DOMContentLoaded and window load events). Deferred scripts are executed in the same order they were printed/added in the DOM, unlike asynchronous scripts.'strategy' => 'async' to the $args parameter.async script 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 the DOMContentLoaded event.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.
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.
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 );
}
© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_enqueue_script