Make WordPress Core


Ignore:
Timestamp:
06/26/2023 01:40:31 PM (8 months ago)
Author:
joemcgill
Message:

Script Loader: Add support for HTML 5 "async" and "defer" attributes.

This allows developers to register scripts with an intended loading strategy by changing the $in_footer parameter of wp_register_script and wp_enqueue_script to an array that accepts both an in_footer and strategy argument. If present, the loading strategy attribute will be added to the script tag when that script is printed to the page as long as it is not a dependency of any blocking scripts, including any inline scripts attached to the script or any of its dependents.

Props 10upsimon, thekt12, westonruter, costdev, flixos90, spacedmonkey, adamsilverstein, azaozz, mukeshpanchal27, mor10, scep, wpnook, vanaf1979, Otto42.
Fixes #12009.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.wp-scripts.php

    r55732 r56033  
    158158 * @since 2.1.0
    159159 * @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.
    160161 *
    161162 * @param string           $handle    Name of the script. Should be unique.
     
    167168 *                                    number is automatically added equal to current installed WordPress version.
    168169 *                                    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  *                                    Default 'false'.
     170 * @param array|bool       $args     {
     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 * }
    171177 * @return bool Whether the script has been registered. True on success, false on failure.
    172178 */
    173 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
     179function 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    }
    174185    _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    175186
     
    177188
    178189    $registered = $wp_scripts->add( $handle, $src, $deps, $ver );
    179     if ( $in_footer ) {
     190    if ( ! empty( $args['in_footer'] ) ) {
    180191        $wp_scripts->add_data( $handle, 'group', 1 );
    181192    }
    182 
     193    if ( ! empty( $args['strategy'] ) ) {
     194        $wp_scripts->add_data( $handle, 'strategy', $args['strategy'] );
     195    }
    183196    return $registered;
    184197}
     
    332345 *
    333346 * @since 2.1.0
     347 * @since 6.3.0 The $in_footer parameter of type boolean was overloaded to be an $args parameter of type array.
    334348 *
    335349 * @param string           $handle    Name of the script. Should be unique.
     
    341355 *                                    number is automatically added equal to current installed WordPress version.
    342356 *                                    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>`.
    344  *                                    Default 'false'.
    345  */
    346 function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $in_footer = false ) {
     357 * @param array|bool       $args     {
     358 *      Optional. An array of additional script loading strategies. Default empty array.
     359 *      Otherwise, it may be a boolean in which case it determines whether the script is printed in the footer. Default false.
     360 *
     361 *      @type string    $strategy     Optional. If provided, may be either 'defer' or 'async'.
     362 *      @type bool      $in_footer    Optional. Whether to print the script in the footer. Default 'false'.
     363 * }
     364 */
     365function wp_enqueue_script( $handle, $src = '', $deps = array(), $ver = false, $args = array() ) {
    347366    _wp_scripts_maybe_doing_it_wrong( __FUNCTION__, $handle );
    348367
    349368    $wp_scripts = wp_scripts();
    350369
    351     if ( $src || $in_footer ) {
     370    if ( $src || ! empty( $args ) ) {
    352371        $_handle = explode( '?', $handle );
     372        if ( ! is_array( $args ) ) {
     373            $args = array(
     374                'in_footer' => (bool) $args,
     375            );
     376        }
    353377
    354378        if ( $src ) {
    355379            $wp_scripts->add( $_handle[0], $src, $deps, $ver );
    356380        }
    357 
    358         if ( $in_footer ) {
     381        if ( ! empty( $args['in_footer'] ) ) {
    359382            $wp_scripts->add_data( $_handle[0], 'group', 1 );
     383        }
     384        if ( ! empty( $args['strategy'] ) ) {
     385            $wp_scripts->add_data( $_handle[0], 'strategy', $args['strategy'] );
    360386        }
    361387    }
Note: See TracChangeset for help on using the changeset viewer.