60 '<code>' . $handle . '</code>' |
60 '<code>' . $handle . '</code>' |
61 ); |
61 ); |
62 } |
62 } |
63 |
63 |
64 _doing_it_wrong( |
64 _doing_it_wrong( |
65 $function, |
65 $function_name, |
66 $message, |
66 $message, |
67 '3.3.0' |
67 '3.3.0' |
68 ); |
68 ); |
69 } |
69 } |
70 |
70 |
71 /** |
71 /** |
72 * Prints scripts in document head that are in the $handles queue. |
72 * Prints scripts in document head that are in the $handles queue. |
73 * |
73 * |
74 * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load, |
74 * Called by admin-header.php and {@see 'wp_head'} hook. Since it is called by wp_head on every page load, |
75 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed. |
75 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed. |
76 * Makes use of already-instantiated $wp_scripts global if present. Use provided {@see 'wp_print_scripts'} |
76 * Makes use of already-instantiated `$wp_scripts` global if present. Use provided {@see 'wp_print_scripts'} |
77 * hook to register/enqueue new scripts. |
77 * hook to register/enqueue new scripts. |
78 * |
78 * |
79 * @see WP_Scripts::do_item() |
79 * @see WP_Scripts::do_item() |
|
80 * @since 2.1.0 |
|
81 * |
80 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
82 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
81 * |
83 * |
82 * @since 2.1.0 |
84 * @param string|string[]|false $handles Optional. Scripts to be printed. Default 'false'. |
83 * |
|
84 * @param string|bool|array $handles Optional. Scripts to be printed. Default 'false'. |
|
85 * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. |
85 * @return string[] On success, an array of handles of processed WP_Dependencies items; otherwise, an empty array. |
86 */ |
86 */ |
87 function wp_print_scripts( $handles = false ) { |
87 function wp_print_scripts( $handles = false ) { |
88 global $wp_scripts; |
88 global $wp_scripts; |
89 |
89 |
146 |
146 |
147 return wp_scripts()->add_inline_script( $handle, $data, $position ); |
147 return wp_scripts()->add_inline_script( $handle, $data, $position ); |
148 } |
148 } |
149 |
149 |
150 /** |
150 /** |
151 * Register a new script. |
151 * Registers a new script. |
152 * |
152 * |
153 * Registers a script to be enqueued later using the wp_enqueue_script() function. |
153 * Registers a script to be enqueued later using the wp_enqueue_script() function. |
154 * |
154 * |
155 * @see WP_Dependencies::add() |
155 * @see WP_Dependencies::add() |
156 * @see WP_Dependencies::add_data() |
156 * @see WP_Dependencies::add_data() |
157 * |
157 * |
158 * @since 2.1.0 |
158 * @since 2.1.0 |
159 * @since 4.3.0 A return value was added. |
159 * @since 4.3.0 A return value was added. |
|
160 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. |
160 * |
161 * |
161 * @param string $handle Name of the script. Should be unique. |
162 * @param string $handle Name of the script. Should be unique. |
162 * @param string|bool $src Full URL of the script, or path of the script relative to the WordPress root directory. |
163 * @param string|false $src Full URL of the script, or path of the script relative to the WordPress root directory. |
163 * If source is set to false, script is an alias of other scripts it depends on. |
164 * If source is set to false, script is an alias of other scripts it depends on. |
164 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
165 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
165 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
166 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
166 * as a query string for cache busting purposes. If version is set to false, a version |
167 * as a query string for cache busting purposes. If version is set to false, a version |
167 * number is automatically added equal to current installed WordPress version. |
168 * number is automatically added equal to current installed WordPress version. |
168 * If set to null, no version is added. |
169 * If set to null, no version is added. |
169 * @param bool $in_footer Optional. Whether to enqueue the script before `</body>` instead of in the `<head>`. |
170 * @param array|bool $args { |
170 * Default 'false'. |
171 * Optional. An array of additional script loading strategies. Default empty array. |
|
172 * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. |
|
173 * |
|
174 * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. |
|
175 * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. |
|
176 * } |
171 * @return bool Whether the script has been registered. True on success, false on failure. |
177 * @return bool Whether the script has been registered. True on success, false on failure. |
172 */ |
178 */ |
173 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) { |
179 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $args = array() ) { |
|
180 if ( ! is_array( $args ) ) { |
|
181 $args = array( |
|
182 'in_footer' => (bool) $args, |
|
183 ); |
|
184 } |
174 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
185 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
175 |
186 |
176 $wp_scripts = wp_scripts(); |
187 $wp_scripts = wp_scripts(); |
177 |
188 |
178 $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); |
189 $registered = $wp_scripts->add( $handle, $src, $deps, $ver ); |
179 if ( $in_footer ) { |
190 if ( ! empty( $args['in_footer'] ) ) { |
180 $wp_scripts->add_data( $handle, 'group', 1 ); |
191 $wp_scripts->add_data( $handle, 'group', 1 ); |
181 } |
192 } |
182 |
193 if ( ! empty( $args['strategy'] ) ) { |
|
194 $wp_scripts->add_data( $handle, 'strategy', $args['strategy'] ); |
|
195 } |
183 return $registered; |
196 return $registered; |
184 } |
197 } |
185 |
198 |
186 /** |
199 /** |
187 * Localize a script. |
200 * Localizes a script. |
188 * |
201 * |
189 * Works only if the script has already been registered. |
202 * Works only if the script has already been registered. |
190 * |
203 * |
191 * Accepts an associative array $l10n and creates a JavaScript object: |
204 * Accepts an associative array `$l10n` and creates a JavaScript object: |
192 * |
205 * |
193 * "$object_name" = { |
206 * "$object_name": { |
194 * key: value, |
207 * key: value, |
195 * key: value, |
208 * key: value, |
196 * ... |
209 * ... |
197 * } |
210 * } |
198 * |
211 * |
199 * @see WP_Scripts::localize() |
212 * @see WP_Scripts::localize() |
200 * @link https://core.trac.wordpress.org/ticket/11520 |
213 * @link https://core.trac.wordpress.org/ticket/11520 |
201 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
|
202 * |
214 * |
203 * @since 2.2.0 |
215 * @since 2.2.0 |
204 * |
216 * |
205 * @todo Documentation cleanup |
217 * @todo Documentation cleanup |
206 * |
218 * |
209 * Example: '/[a-zA-Z0-9_]+/'. |
221 * Example: '/[a-zA-Z0-9_]+/'. |
210 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. |
222 * @param array $l10n The data itself. The data can be either a single or multi-dimensional array. |
211 * @return bool True if the script was successfully localized, false otherwise. |
223 * @return bool True if the script was successfully localized, false otherwise. |
212 */ |
224 */ |
213 function wp_localize_script( $handle, $object_name, $l10n ) { |
225 function wp_localize_script( $handle, $object_name, $l10n ) { |
214 global $wp_scripts; |
226 $wp_scripts = wp_scripts(); |
215 |
|
216 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
|
217 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
|
218 return false; |
|
219 } |
|
220 |
227 |
221 return $wp_scripts->localize( $handle, $object_name, $l10n ); |
228 return $wp_scripts->localize( $handle, $object_name, $l10n ); |
222 } |
229 } |
223 |
230 |
224 /** |
231 /** |
225 * Sets translated strings for a script. |
232 * Sets translated strings for a script. |
226 * |
233 * |
227 * Works only if the script has already been registered. |
234 * Works only if the script has already been registered. |
228 * |
235 * |
229 * @see WP_Scripts::set_translations() |
236 * @see WP_Scripts::set_translations() |
230 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
|
231 * |
|
232 * @since 5.0.0 |
237 * @since 5.0.0 |
233 * @since 5.1.0 The `$domain` parameter was made optional. |
238 * @since 5.1.0 The `$domain` parameter was made optional. |
|
239 * |
|
240 * @global WP_Scripts $wp_scripts The WP_Scripts object for printing scripts. |
234 * |
241 * |
235 * @param string $handle Script handle the textdomain will be attached to. |
242 * @param string $handle Script handle the textdomain will be attached to. |
236 * @param string $domain Optional. Text domain. Default 'default'. |
243 * @param string $domain Optional. Text domain. Default 'default'. |
237 * @param string $path Optional. The full file path to the directory containing translation files. |
244 * @param string $path Optional. The full file path to the directory containing translation files. |
238 * @return bool True if the text domain was successfully localized, false otherwise. |
245 * @return bool True if the text domain was successfully localized, false otherwise. |
239 */ |
246 */ |
240 function wp_set_script_translations( $handle, $domain = 'default', $path = null ) { |
247 function wp_set_script_translations( $handle, $domain = 'default', $path = '' ) { |
241 global $wp_scripts; |
248 global $wp_scripts; |
242 |
249 |
243 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
250 if ( ! ( $wp_scripts instanceof WP_Scripts ) ) { |
244 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
251 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
245 return false; |
252 return false; |
320 |
327 |
321 wp_scripts()->remove( $handle ); |
328 wp_scripts()->remove( $handle ); |
322 } |
329 } |
323 |
330 |
324 /** |
331 /** |
325 * Enqueue a script. |
332 * Enqueues a script. |
326 * |
333 * |
327 * Registers the script if $src provided (does NOT overwrite), and enqueues it. |
334 * Registers the script if `$src` provided (does NOT overwrite), and enqueues it. |
328 * |
335 * |
329 * @see WP_Dependencies::add() |
336 * @see WP_Dependencies::add() |
330 * @see WP_Dependencies::add_data() |
337 * @see WP_Dependencies::add_data() |
331 * @see WP_Dependencies::enqueue() |
338 * @see WP_Dependencies::enqueue() |
332 * |
339 * |
333 * @since 2.1.0 |
340 * @since 2.1.0 |
|
341 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array. |
334 * |
342 * |
335 * @param string $handle Name of the script. Should be unique. |
343 * @param string $handle Name of the script. Should be unique. |
336 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
344 * @param string $src Full URL of the script, or path of the script relative to the WordPress root directory. |
337 * Default empty. |
345 * Default empty. |
338 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
346 * @param string[] $deps Optional. An array of registered script handles this script depends on. Default empty array. |
339 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
347 * @param string|bool|null $ver Optional. String specifying script version number, if it has one, which is added to the URL |
340 * as a query string for cache busting purposes. If version is set to false, a version |
348 * as a query string for cache busting purposes. If version is set to false, a version |
341 * number is automatically added equal to current installed WordPress version. |
349 * number is automatically added equal to current installed WordPress version. |
342 * If set to null, no version is added. |
350 * If set to null, no version is added. |
343 * @param bool $in_footer Optional. Whether to enqueue the script before `</body>` instead of in the `<head>`. |
351 * @param array|bool $args { |
344 * Default 'false'. |
352 * Optional. An array of additional script loading strategies. Default empty array. |
345 */ |
353 * Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false. |
346 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) { |
354 * |
|
355 * @type string $strategy Optional. If provided, may be either 'defer' or 'async'. |
|
356 * @type bool $in_footer Optional. Whether to print the script in the footer. Default 'false'. |
|
357 * } |
|
358 */ |
|
359 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $args = array() ) { |
347 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
360 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
348 |
361 |
349 $wp_scripts = wp_scripts(); |
362 $wp_scripts = wp_scripts(); |
350 |
363 |
351 if ( $src || $in_footer ) { |
364 if ( $src || ! empty( $args ) ) { |
352 $_handle = explode( '?', $handle ); |
365 $_handle = explode( '?', $handle ); |
|
366 if ( ! is_array( $args ) ) { |
|
367 $args = array( |
|
368 'in_footer' => (bool) $args, |
|
369 ); |
|
370 } |
353 |
371 |
354 if ( $src ) { |
372 if ( $src ) { |
355 $wp_scripts->add( $_handle[0], $src, $deps, $ver ); |
373 $wp_scripts->add( $_handle[0], $src, $deps, $ver ); |
356 } |
374 } |
357 |
375 if ( ! empty( $args['in_footer'] ) ) { |
358 if ( $in_footer ) { |
|
359 $wp_scripts->add_data( $_handle[0], 'group', 1 ); |
376 $wp_scripts->add_data( $_handle[0], 'group', 1 ); |
360 } |
377 } |
|
378 if ( ! empty( $args['strategy'] ) ) { |
|
379 $wp_scripts->add_data( $_handle[0], 'strategy', $args['strategy'] ); |
|
380 } |
361 } |
381 } |
362 |
382 |
363 $wp_scripts->enqueue( $handle ); |
383 $wp_scripts->enqueue( $handle ); |
364 } |
384 } |
365 |
385 |
366 /** |
386 /** |
367 * Remove a previously enqueued script. |
387 * Removes a previously enqueued script. |
368 * |
388 * |
369 * @see WP_Dependencies::dequeue() |
389 * @see WP_Dependencies::dequeue() |
370 * |
390 * |
371 * @since 3.1.0 |
391 * @since 3.1.0 |
372 * |
392 * |
387 * |
407 * |
388 * @since 2.8.0 |
408 * @since 2.8.0 |
389 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list. |
409 * @since 3.5.0 'enqueued' added as an alias of the 'queue' list. |
390 * |
410 * |
391 * @param string $handle Name of the script. |
411 * @param string $handle Name of the script. |
392 * @param string $list Optional. Status of the script to check. Default 'enqueued'. |
412 * @param string $status Optional. Status of the script to check. Default 'enqueued'. |
393 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
413 * Accepts 'enqueued', 'registered', 'queue', 'to_do', and 'done'. |
394 * @return bool Whether the script is queued. |
414 * @return bool Whether the script is queued. |
395 */ |
415 */ |
396 function wp_script_is( $handle, $list = 'enqueued' ) { |
416 function wp_script_is( $handle, $status = 'enqueued' ) { |
397 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
417 _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle ); |
398 |
418 |
399 return (bool) wp_scripts()->query( $handle, $list ); |
419 return (bool) wp_scripts()->query( $handle, $status ); |
400 } |
420 } |
401 |
421 |
402 /** |
422 /** |
403 * Add metadata to a script. |
423 * Adds metadata to a script. |
404 * |
424 * |
405 * Works only if the script has already been registered. |
425 * Works only if the script has already been registered. |
406 * |
426 * |
407 * Possible values for $key and $value: |
427 * Possible values for $key and $value: |
408 * 'conditional' string Comments for IE 6, lte IE 7, etc. |
428 * 'conditional' string Comments for IE 6, lte IE 7, etc. |