wp_enqueue_script() WordPress Function
The wp_enqueue_script() function is used to load a JavaScript file into a WordPress page. This function should be used when you need to load a JavaScript file on a WordPress page.
wp_enqueue_script( string $handle, string $src = '', string[] $deps = array(), string|bool|null $ver = false, bool $in_footer = false ) #
Enqueue a script.
Contents
Description
Registers the script if $src provided (does NOT overwrite), and enqueues it.
See also
Parameters
- $handle
(string)(Required)Name of the script. Should be unique.
- $src
(string)(Optional)Full URL of the script, or path of the script relative to the WordPress root directory.
Default value: ''
- $deps
(string[])(Optional) An array of registered script handles this script depends on.
Default value: array()
- $ver
(string|bool|null)(Optional) 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 value: false
- $in_footer
(bool)(Optional) Whether to enqueue the script before
</body>
instead of in the<head>
. Default 'false'.Default value: false
More Information
Usage
wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer );
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.
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
File: wp-includes/functions.wp-scripts.php
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); $wp_scripts = wp_scripts(); if ( $src || $in_footer ) { $_handle = explode( '?', $handle ); if ( $src ) { $wp_scripts->add( $_handle[0], $src, $deps, $ver ); } if ( $in_footer ) { $wp_scripts->add_data( $_handle[0], 'group', 1 ); } } $wp_scripts->enqueue( $handle ); }
Expand full source codeCollapse full source codeView on TracView on GitHub
Changelog
Version | Description |
---|---|
2.1.0 | Introduced. |