3 * Dependencies API: Scripts functions
8 * @subpackage Dependencies
12 * Initialize $wp_scripts if it has not been set.
14 * @global WP_Scripts $wp_scripts
18 * @return WP_Scripts WP_Scripts instance.
20 function wp_scripts() {
22 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
23 $wp_scripts = new WP_Scripts();
29 * Helper function to output a _doing_it_wrong message when applicable.
34 * @param string $function Function name.
36 function _wp_scripts_maybe_doing_it_wrong( $function ) {
37 if ( did_action( 'init' ) ) {
41 _doing_it_wrong( $function, sprintf(
42 __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
43 '<code>wp_enqueue_scripts</code>',
44 '<code>admin_enqueue_scripts</code>',
45 '<code>login_enqueue_scripts</code>'
50 * Print scripts in document head that are in the $handles queue.
52 * Called by admin-header.php and wp_head hook. Since it is called by wp_head on every page load,
53 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
54 * Makes use of already-instantiated $wp_scripts global if present. Use provided wp_print_scripts
55 * hook to register/enqueue new scripts.
57 * @see WP_Scripts::do_items()
58 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
62 * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'.
63 * @return array On success, a processed array of WP_Dependencies items; otherwise, an empty array.
65 function wp_print_scripts( $handles = false ) {
67 * Fires before scripts in the $handles queue are printed.
71 do_action( 'wp_print_scripts' );
72 if ( '' === $handles ) { // for wp_head
76 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
79 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
81 return array(); // No need to instantiate if nothing is there.
85 return wp_scripts()->do_items( $handles );
89 * Adds extra code to a registered script.
91 * Code will only be added if the script in already in the queue.
92 * Accepts a string $data containing the Code. If two or more code blocks
93 * are added to the same script $handle, they will be printed in the order
94 * they were added, i.e. the latter added code can redeclare the previous.
98 * @see WP_Scripts::add_inline_script()
100 * @param string $handle Name of the script to add the inline script to.
101 * @param string $data String containing the javascript to be added.
102 * @param string $position Optional. Whether to add the inline script before the handle
103 * or after. Default 'after'.
104 * @return bool True on success, false on failure.
106 function wp_add_inline_script( $handle, $data, $position = 'after' ) {
107 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
109 if ( false !== stripos( $data, '</script>' ) ) {
110 _doing_it_wrong( __FUNCTION__, __( 'Do not pass script tags to wp_add_inline_script().' ), '4.5.0' );
111 $data = trim( preg_replace( '#<script[^>]*>(.*)</script>#is', '$1', $data ) );
114 return wp_scripts()->add_inline_script( $handle, $data, $position );
118 * Register a new script.
120 * Registers a script to be enqueued later using the wp_enqueue_script() function.
122 * @see WP_Dependencies::add()
123 * @see WP_Dependencies::add_data()
126 * @since 4.3.0 A return value was added.
128 * @param string $handle Name of the script. Should be unique.
129 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
130 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
131 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
132 * as a query string for cache busting purposes. If version is set to false, a version
133 * number is automatically added equal to current installed WordPress version.
134 * If set to null, no version is added.
135 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
137 * @return bool Whether the script has been registered. True on success, false on failure.
139 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
140 $wp_scripts = wp_scripts();
141 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
143 $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
145 $wp_scripts->add_data( $handle, 'group', 1 );
154 * Works only if the script has already been added.
156 * Accepts an associative array $l10n and creates a JavaScript object:
165 * @see WP_Dependencies::localize()
166 * @link https://core.trac.wordpress.org/ticket/11520
167 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts.
171 * @todo Documentation cleanup
173 * @param string $handle Script handle the data will be attached to.
174 * @param string $object_name Name for the JavaScript object. Passed directly, so it should be qualified JS variable.
175 * Example: '/[a-zA-Z0-9_]+/'.
176 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array.
177 * @return bool True if the script was successfully localized, false otherwise.
179 function wp_localize_script( $handle, $object_name, $l10n ) {
181 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) {
182 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
186 return $wp_scripts->localize( $handle, $object_name, $l10n );
190 * Remove a registered script.
192 * Note: there are intentional safeguards in place to prevent critical admin scripts,
193 * such as jQuery core, from being unregistered.
195 * @see WP_Dependencies::remove()
199 * @param string $handle Name of the script to be removed.
201 function wp_deregister_script( $handle ) {
202 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
205 * Do not allow accidental or negligent de-registering of critical scripts in the admin.
206 * Show minimal remorse if the correct hook is used.
208 $current_filter = current_filter();
209 if ( ( is_admin() && 'admin_enqueue_scripts' !== $current_filter ) ||
210 ( 'wp-login.php' === $GLOBALS['pagenow'] && 'login_enqueue_scripts' !== $current_filter )
213 'jquery', 'jquery-core', 'jquery-migrate', 'jquery-ui-core', 'jquery-ui-accordion',
214 'jquery-ui-autocomplete', 'jquery-ui-button', 'jquery-ui-datepicker', 'jquery-ui-dialog',
215 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-menu', 'jquery-ui-mouse',
216 'jquery-ui-position', 'jquery-ui-progressbar', 'jquery-ui-resizable', 'jquery-ui-selectable',
217 'jquery-ui-slider', 'jquery-ui-sortable', 'jquery-ui-spinner', 'jquery-ui-tabs',
218 'jquery-ui-tooltip', 'jquery-ui-widget', 'underscore', 'backbone',
221 if ( in_array( $handle, $no ) ) {
222 $message = sprintf( __( 'Do not deregister the %1$s script in the administration area. To target the front-end theme, use the %2$s hook.' ),
223 "<code>$handle</code>", '<code>wp_enqueue_scripts</code>' );
224 _doing_it_wrong( __FUNCTION__, $message, '3.6' );
229 wp_scripts()->remove( $handle );
235 * Registers the script if $src provided (does NOT overwrite), and enqueues it.
237 * @see WP_Dependencies::add()
238 * @see WP_Dependencies::add_data()
239 * @see WP_Dependencies::enqueue()
243 * @param string $handle Name of the script. Should be unique.
244 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory.
245 * @param array $deps Optional. An array of registered script handles this script depends on. Default empty array.
246 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL
247 * as a query string for cache busting purposes. If version is set to false, a version
248 * number is automatically added equal to current installed WordPress version.
249 * If set to null, no version is added.
250 * @param bool $in_footer Optional. Whether to enqueue the script before </body> instead of in the <head>.
253 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
254 $wp_scripts = wp_scripts();
256 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
259 if ( $src || $in_footer ) {
260 $_handle = explode( '?', $handle );
263 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
267 $wp_scripts->add_data( $_handle[0], 'group', 1 );
271 $wp_scripts->enqueue( $handle );
275 * Remove a previously enqueued script.
277 * @see WP_Dependencies::dequeue()
281 * @param string $handle Name of the script to be removed.
283 function wp_dequeue_script( $handle ) {
284 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
286 wp_scripts()->dequeue( $handle );
290 * Check whether a script has been added to the queue.
293 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list.
295 * @param string $handle Name of the script.
296 * @param string $list Optional. Status of the script to check. Default 'enqueued'.
297 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'.
298 * @return bool Whether the script is queued.
300 function wp_script_is( $handle, $list = 'enqueued' ) {
301 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__ );
303 return (bool) wp_scripts()->query( $handle, $list );
307 * Add metadata to a script.
309 * Works only if the script has already been added.
311 * Possible values for $key and $value:
312 * 'conditional' string Comments for IE 6, lte IE 7, etc.
316 * @see WP_Dependency::add_data()
318 * @param string $handle Name of the script.
319 * @param string $key Name of data point for which we're storing a value.
320 * @param mixed $value String containing the data to be added.
321 * @return bool True on success, false on failure.
323 function wp_script_add_data( $handle, $key, $value ){
324 return wp_scripts()->add_data( $handle, $key, $value );