Description

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.

Usage

<?php wp_enqueue_script( $handle, $src, $deps, $ver, $in_footer ); ?>
See Notes for information about what action hooks should be used to call the function.

Parameters

$handle

(string) (required) Name used as a handle for the script. As a special case, if the string contains a '?' character, the preceding part of the string refers to the registered handle, and the succeeding part is appended to the URL as a query string. A version must be used with this special case.

(array) (optional) Array of the handles of all the registered scripts that this script depends on, that is the scripts that must be loaded before this script. This parameter is only required when the script with the given $handle has not been already registered using wp_register_script(). Default handles are all in lower case.

Default: array()

$ver

(string) (optional) String specifying the script version number, if it has one, which is concatenated to the end of the path as a query string. If no version is specified or set to false, then WordPress automatically adds a version number equal to the current version of WordPress you are running. If set to null no version is added. This parameter is used to ensure that the correct version is sent to the client regardless of caching, and so should be included if a version number is available and makes sense for the script.

Default: false

$in_footer

(boolean) (optional) Normally, scripts are placed in <head> of the HTML document. If this parameter is true, the script is placed before the </body> end tag. This requires the theme to have the wp_footer() template tag in the appropriate place.

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.

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:

Load a Script from a Child Theme without Dependencies

Register and enqueue the script in the same callback function with no dependencies, in the footer. See wp_register_script() for details. In this example, google_analytics_object.js is the Google Analytics tracking code (provided by Google) in a file.

In the noConflict() mode, the global $ shortcut for jQuery is not available, so you can still use:

jQuery(document).ready(function(){
jQuery(#somefunction) ...
});

but the following will either throw an error, or use the $ shortcut as assigned by another library.

$(document).ready(function(){
$(#somefunction) ...
});

However, if you really like the short $ instead of jQuery, you can use the following wrapper around your code:

jQuery(document).ready(function($) {
// Inside of this function, $() will work as an alias for jQuery()
// and other libraries also using $ will not be accessible under this shortcut
});

That wrapper will cause your code to be executed when the DOM is fully constructed. If, for some reason, you want your code to execute immediately instead of waiting for the DOM ready event, then you can use this wrapper method instead:

(function($) {
// Inside of this function, $() will work as an alias for jQuery()
// and other libraries also using $ will not be accessible under this shortcut
})(jQuery);

Alternatively, you can always reasign jQuery to another shortcut of your choice and leave the $ shorcut to other libraries:

var $j = jQuery;

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.

Note that in Version 3.5, WordPress changed its naming convention for minified scripts and styles. Before, minified scripts and styles had the .js and .css extensions, unminified had .dev.js and .dev.css. However, following the change, the extensions are .min.js and .min.css for minified files, .js and .css for unminified, respectively.

Prior to Version 3.3, the function will have no effect if it is called using the wp_head or wp_print_scripts action hooks or later, as this is too late to enqueue the files even if the $in_footer parameter is set to true.

As of Version 3.3, the function can be called mid-page (before the wp_footer() template tag) or using the wp_head action hook. This will place the script in the footer as if the $in_footer parameter was set to true.

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.