wp_enqueue_script( string $handle, string $src = '', array $deps = array(), string|bool|null $ver = false, bool $in_footer = false )
Enqueue a script.
Contents
Description Description
Registers the script if $src provided (does NOT overwrite), and enqueues it.
See also See also
Parameters 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
-
(array) (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
Source Source
File: wp-includes/functions.wp-scripts.php
function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { $wp_scripts = wp_scripts(); _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ ); 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 code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
2.1.0 | Introduced. |
More Information More Information
Usage 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 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 Included and Registered by WordPress Default Scripts Included and Registered by WordPress
By default, WordPress installation includes many popular 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 * |
---|---|---|
Image Cropper | Image cropper (not used in core, see jcrop) | |
Jcrop | jcrop | |
SWFObject | swfobject | |
SWFUpload | swfupload | |
SWFUpload Degrade | swfupload-degrade | |
SWFUpload Queue | swfupload-queue | |
SWFUpload Handlers | swfupload-handlers | |
jQuery | jquery | json2 (for AJAX calls) |
jQuery Form | jquery-form | jquery |
jQuery Color | jquery-color | jquery |
jQuery Masonry | jquery-masonry | jquery |
Masonry (native Javascript) | masonry | imagesloaded |
jQuery UI Core | jquery-ui-core | jquery |
jQuery UI Widget | jquery-ui-widget | jquery |
jQuery UI Accordion | jquery-ui-accordion | jquery |
jQuery UI Autocomplete | jquery-ui-autocomplete | jquery |
jQuery UI Button | jquery-ui-button | jquery |
jQuery UI Datepicker | jquery-ui-datepicker | jquery |
jQuery UI Dialog | jquery-ui-dialog | jquery |
jQuery UI Draggable | jquery-ui-draggable | jquery |
jQuery UI Droppable | jquery-ui-droppable | jquery |
jQuery UI Menu | jquery-ui-menu | jquery |
jQuery UI Mouse | jquery-ui-mouse | jquery |
jQuery UI Position | jquery-ui-position | jquery |
jQuery UI Progressbar | jquery-ui-progressbar | jquery |
jQuery UI Selectable | jquery-ui-selectable | jquery |
jQuery UI Resizable | jquery-ui-resizable | jquery |
jQuery UI Selectmenu | jquery-ui-selectmenu | jquery |
jQuery UI Sortable | jquery-ui-sortable | jquery |
jQuery UI Slider | jquery-ui-slider | jquery |
jQuery UI Spinner | jquery-ui-spinner | jquery |
jQuery UI Tooltips | jquery-ui-tooltip | jquery |
jQuery UI Tabs | jquery-ui-tabs | 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 UI Effects – Pulsate | jquery-effects-pulsate | jquery-effects-core |
jQuery UI Effects – Scale | jquery-effects-scale | jquery-effects-core |
jQuery UI Effects – Shake | jquery-effects-shake | jquery-effects-core |
jQuery UI Effects – Slide | jquery-effects-slide | jquery-effects-core |
jQuery UI Effects – Transfer | jquery-effects-transfer | jquery-effects-core |
MediaElement.js (WP 3.6+) | wp-mediaelement | jquery |
jQuery Schedule | schedule | jquery |
jQuery Suggest | suggest | jquery |
ThickBox | thickbox | |
jQuery HoverIntent | hoverIntent | jquery |
jQuery Hotkeys | jquery-hotkeys | jquery |
Simple AJAX Code-Kit | sack | |
QuickTags | quicktags | |
Iris (Colour picker) | iris | jquery |
Farbtastic (deprecated) | farbtastic | jquery |
ColorPicker (deprecated) | colorpicker | jquery |
Tiny MCE | tiny_mce | |
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 | |
Plupload Core | plupload | |
Plupload All Runtimes | plupload-all | |
Plupload HTML4 | plupload-html4 | |
Plupload HTML5 | plupload-html5 | |
Plupload Flash | plupload-flash | |
Plupload Silverlight | plupload-silverlight | |
Underscore js | underscore | |
Backbone js | backbone | |
imagesLoaded | imagesloaded |
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.
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
Never worry about cache again!
This is a little trick I’ve picked up along the way.
The version of the .js and .css files is made from the time of it’s last update.
Expand full source codeCollapse full source code
That way, you will always use the cached versions, except in case the files were changed in the meanwhile, which is the most optimum scenario.
When you enqueue script that is dependent on jQuery, note that the jQuery in WordPress runs in noConflict mode, which means you cannot use the common
$
alias. You must use the fulljQuery
instead. Alternately, place your code using the$
shortcut inside a noConflict wrapper.Using a Hook
Enqueue both scripts and styles from a single action hook.
Link a Theme Script Which Depends on jQuery
JavaScript files included in themes often require another JavaScript file to be loaded in advance to use its functions or variables. Using the $deps parameter, the wp_enqueue_script() and wp_register_script() functions allows you to mark dependencies when registering a new script. This will cause WordPress to automatically link these dependencies to the HTML page before the new script is linked. If the handles of these dependencies are not registered, WordPress will not link the new script. This example requires the jQuery library for the custom_script.js theme script:
I want to add a note to bcworkz comment. When using jquery, in general, this is how the code in a file should be formatted:
It’s a common practice that ensures jQuery runs in no conflict mode and in strict mode.
Strict mode helps out in a couple ways:
Link the script.aculo.us Library
The following is an example of basic usage which links the script.aculo.us library already included and registered by WordPress with the scriptaculous handle.
The above example links the script.aculo.us library only on the front-end. If the library was needed within the administration screens, you could use the admin_enqueue_scripts action hook instead, however, this would enqueue it on all the administration screens, which often leads to plugin/core conflicts, ultimately breaking the WordPress administration screens experience. Instead, you should only link it on the individual screens you need it, see the Link Scripts Only on a Plugin Administration Screen section for an example of that.
When registering and enqueueing scripts, you don’t need to call
wp_register_script()
andwp_enqueue_script()
. Just callwp_enqueue_script()
.This:
Should simply be:
Only register if you don’t know if you’re going to load it immediately.
Because of the work on https://core.trac.wordpress.org/ticket/9346, it’s actually safe to call
after the hook “wp_enqueue_scripts” and before the footer, eg when rendering shortcodes or rendering a post’s body.
Enqueueing stylesheets (css) in this same method has some issues though.
Scribu gave an example of a shortcode enqueueing a script here: http://scribu.net/wordpress/conditional-script-loading-revisited.html
This is not documented, but calling this function with
$in_footer
being `false` actually registers the script in general as a dependency that should be resolved and output, not just for header. Then, when outputting scripts enqueued for the footer, only the scripts that have not been included yet will be output. This leads to behaviour where a script that is enqueued supposedly in the header will appear in the footer if enqueued after the header has already been process. Therefore,$in_footer
should actually read and be understood as$in_footer_only
.Because this and
wp_enqueue_style
usesWP_Scripts::enqueue()
, which can take an array of handles as it’s first argument, you can also pass an array of handles intowp_enqueue_script/style
:See Also:
wp_enqueue_style
This documentation should really do a better job defining what a “script” is… Because: is this function meant for enqueueing a .php or html script? No!
That may seem obvious to a lot of developers, but if you are looking at this documentation, you should be getting some clarity as to what this function should be used for, so I’m writing this to help.
Apparently, in this case, the term “script” is being used as a program that is not being evaluated by the server, but rather a program that needs to be passed to the client (and evaluated by the browser). Thus in this case, a PHP file that is expected to be evaluated by the server would not be considered a script (even though a lot of people might call it one) and thus it should not be included with wp_enqueue_script(), but included using require_once() or possibly include_once() instead.
For who have the same question of @damiankaelgreen:
In this case script is the file called in one HTML tag script which is used to define a client-side script (JavaScript).
For include PHP files, please use get_template_part().
This example link of custom stylesheet and script with file version version
Unmet dependencies will *NOT* be warned
If dependencies (3rd argument) are not met, the script tag is just not printed and no warnings nor errors are shown.
This doesn’t seem like a bug, just expected behavior. A mention about this on the docs would suffice to prevent misunderstandings.
This example will never load
/js/example.js
Expand full source codeCollapse full source code
Link a Plugin Script That Depends on script.aculo.us
This example is intended to be used within a plugin file to register and link a new plugin script that depends on the script.aculo.us library. See the example above for information about dependencies.
Link Scripts Only on a Plugin Administration Screen
This example links a script only on a specific administration screen, as opposed to the scenario described in the paragraph below the code of the first example.
Expand full source codeCollapse full source code
This is the place to report it. I’ve updated the table with your changes. Thanks!