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.


Description

Registers the script if $src provided (does NOT overwrite), and enqueues it.

Top ↑

See also


Top ↑

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


Top ↑

More Information

Top ↑

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.

Top ↑

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.

Top ↑

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 CropperImage cropper (not used in core, see jcrop)
Jcropjcrop0.9.12MIT
SWFObjectswfobject2.2-20120417MIT
SWFUploadswfupload2201-20110113MIT
SWFUpload Degradeswfupload-degrade2201MIT
SWFUpload Queueswfupload-queue2201MIT
SWFUpload Handlersswfupload-handlers2201-20110524MIT
jQueryjqueryjson2 (for AJAX calls)3.6.0MIT + (MIT OR BSD)
jQuery Formjquery-formjquery4.3.0MIT OR LGPLv3
jQuery Colorjquery-colorjquery2.2.0MIT+CC0 + (MIT OR GPLv2)
jQuery Masonryjquery-masonryjquery3.1.2bMIT
Masonry (native Javascript)masonryimagesloaded4.2.2MIT
jQuery UI Corejquery-ui-corejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI WidgetNow part of `jquery-ui-core`jquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Accordionjquery-ui-accordionjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Autocompletejquery-ui-autocompletejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Buttonjquery-ui-buttonjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Datepickerjquery-ui-datepickerjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Dialogjquery-ui-dialogjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Draggablejquery-ui-draggablejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Droppablejquery-ui-droppablejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Menujquery-ui-menujquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Mousejquery-ui-mousejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI PositionNow part of `jquery-ui-core`jquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Progressbarjquery-ui-progressbarjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Selectablejquery-ui-selectablejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Resizablejquery-ui-resizablejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Selectmenujquery-ui-selectmenujquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Sortablejquery-ui-sortablejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Sliderjquery-ui-sliderjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Spinnerjquery-ui-spinnerjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Tooltipsjquery-ui-tooltipjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Tabsjquery-ui-tabsjquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effectsjquery-effects-corejquery1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Blindjquery-effects-blindjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Bouncejquery-effects-bouncejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Clipjquery-effects-clipjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Dropjquery-effects-dropjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Explodejquery-effects-explodejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Fadejquery-effects-fadejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Foldjquery-effects-foldjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Highlightjquery-effects-highlightjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Pulsatejquery-effects-pulsatejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Scalejquery-effects-scalejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Shakejquery-effects-shakejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Slidejquery-effects-slidejquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
jQuery UI Effects – Transferjquery-effects-transferjquery-effects-core1.13.1MIT + CC0 + (MIT OR GPLv2)
MediaElement.js (WP 3.6+)wp-mediaelementjquery4.2.16MIT
jQuery Scheduleschedulejquery20m/1.0.1MIT
jQuery Suggestsuggestjquery1.1-20110113Public domain
ThickBoxthickbox3.1-20121105MIT OR GPLv3
jQuery HoverIntenthoverIntentjquery1.10.1MIT
jQuery Hotkeysjquery-hotkeysjquery0.2.0MIT OR GPLv2
Simple AJAX Code-Kitsack1.6.1X11 License
QuickTagsquicktags1.3LGPL2.1
Iris (Colour picker)irisjquery1.1.1GPLv2
Farbtastic (deprecated)farbtasticjquery1.2GPLv3
ColorPicker (deprecated)colorpickerjqueryv2Author’s own copyright
Tiny MCEwp-tinymce4.9.4LGPL2.1
Autosaveautosave
WordPress AJAX Responsewp-ajax-response
List Manipulationwp-lists
WP Commoncommon
WP Editoreditorremov
WP Editor Functionseditor-functions
AJAX Catajaxcat
Admin Categoriesadmin-categories
Admin Tagsadmin-tags
Admin custom fieldsadmin-custom-fields
Password Strength Meterpassword-strength-meter
Admin Commentsadmin-comments
Admin Usersadmin-users
Admin Formsadmin-forms
XFNxfn
Uploadupload
PostBoxpostbox
Slugslug
Postpost
Pagepage
Linklink
Commentcomment
Threaded Commentscomment-reply
Admin Galleryadmin-gallery
Media Uploadmedia-upload
Admin widgetsadmin-widgets
Word Countword-count
Theme Previewtheme-preview
JSON for JSjson22015-05-03Public domain
Plupload Coreplupload2.1.9GPLv2
Plupload All Runtimesplupload-all2.1.1GPLv2
Plupload HTML4plupload-html42.1.1GPLv2
Plupload HTML5plupload-html52.1.1GPLv2
Plupload Flashplupload-flash2.1.1GPLv2
Plupload Silverlightplupload-silverlight2.1.1GPLv2
Underscore jsunderscore1.13.1MIT
Backbone jsbackbonejquery, underscore1.4.0MIT
imagesLoadedimagesloaded4.1.4MIT
CodeMirrorwp-codemirror5.29.1-alpha-ee20357MIT
imgAreaSelectimgareaselectjquery0.9.8MIT AND GPL
Removed from Core
Script NameHandleRemoved VersionReplaced With
Scriptaculous Rootscriptaculous-rootWP 3.5Google Version
Scriptaculous Builderscriptaculous-builderWP 3.5Google Version
Scriptaculous Drag & Dropscriptaculous-dragdropWP 3.5Google Version
Scriptaculous Effectsscriptaculous-effectsWP 3.5Google Version
Scriptaculous Sliderscriptaculous-sliderWP 3.5Google Version
Scriptaculous Soundscriptaculous-soundWP 3.5Google Version
Scriptaculous Controlsscriptaculous-controlsWP 3.5Google Version
ScriptaculousscriptaculousWP 3.5Google Version
Prototype FrameworkprototypeWP 3.5Google 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.


Top ↑

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


Top ↑

Changelog

Changelog
VersionDescription
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.