diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-scripts.php
index cc88455bc1..f4277c8586 100644
a
|
b
|
class WP_Scripts extends WP_Dependencies { |
351 | 351 | echo $cond_after; |
352 | 352 | } |
353 | 353 | |
| 354 | $async_attr = ''; |
| 355 | $defer_attr = ''; |
| 356 | |
| 357 | if ( false === in_array( $handle, $this->in_footer ) ) { |
| 358 | if( true === apply_filters( 'script_attr_async', $obj->extra['is_async'] ?? false ) ) { |
| 359 | $async_attr = ' async'; |
| 360 | } |
| 361 | |
| 362 | if( true === apply_filters( 'script_attr_defer', $obj->extra['is_defer'] ?? false ) ) { |
| 363 | $defer_attr = ' defer'; |
| 364 | } |
| 365 | } |
| 366 | |
354 | 367 | // A single item may alias a set of items, by having dependencies, but no source. |
355 | 368 | if ( ! $src ) { |
356 | 369 | if ( $inline_script_tag ) { |
… |
… |
class WP_Scripts extends WP_Dependencies { |
385 | 398 | } |
386 | 399 | |
387 | 400 | $tag = $translations . $cond_before . $before_handle; |
388 | | $tag .= sprintf( "<script%s src='%s'></script>\n", $this->type_attr, $src ); |
| 401 | $tag .= sprintf( "<script%s src='%s'%s%s></script>\n", $this->type_attr, $src, $async_attr, $defer_attr ); |
389 | 402 | $tag .= $after_handle . $cond_after; |
390 | 403 | |
391 | 404 | /** |
diff --git a/wp-includes/functions.wp-scripts.php b/wp-includes/functions.wp-scripts.php
index 3eefe4f903..b900fa2b2a 100644
a
|
b
|
function wp_script_is( $handle, $list = 'enqueued' ) { |
389 | 389 | */ |
390 | 390 | function wp_script_add_data( $handle, $key, $value ) { |
391 | 391 | return wp_scripts()->add_data( $handle, $key, $value ); |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Add 'is_async' metadata to a script. |
| 396 | * |
| 397 | * @see WP_Dependencies::add_data() |
| 398 | * |
| 399 | * @param string|array $handles Names of the scripts. |
| 400 | */ |
| 401 | function wp_async_script($handles) { |
| 402 | if('string' === gettype($handles)) { |
| 403 | $handles = [$handles]; |
| 404 | } |
| 405 | |
| 406 | if('array' !== gettype($handles)) { |
| 407 | return; |
| 408 | } |
| 409 | |
| 410 | foreach($handles as $handle ) { |
| 411 | wp_scripts()->add_data( $handle, 'is_async', true ); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * Add 'is_defer' metadata to a script. |
| 417 | * |
| 418 | * @see WP_Dependencies::add_data() |
| 419 | * |
| 420 | * @param string|array $handles Names of the scripts. |
| 421 | */ |
| 422 | function wp_defer_script($handles) { |
| 423 | if('string' === gettype($handles)) { |
| 424 | $handles = [$handles]; |
| 425 | } |
| 426 | |
| 427 | if('array' !== gettype($handles)) { |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | foreach($handles as $handle ) { |
| 432 | wp_scripts()->add_data( $handle, 'is_defer', true ); |
| 433 | } |
392 | 434 | } |