[wp-trac] [WordPress Trac] #12009: Add support for HTML 5 "async" and "defer" attributes

WordPress Trac noreply at wordpress.org
Sat Aug 26 13:29:47 UTC 2023


#12009: Add support for HTML 5 "async" and "defer" attributes
-------------------------------------------+--------------------------
 Reporter:  Otto42                         |       Owner:  joemcgill
     Type:  enhancement                    |      Status:  closed
 Priority:  high                           |   Milestone:  6.3
Component:  Script Loader                  |     Version:  4.6
 Severity:  normal                         |  Resolution:  fixed
 Keywords:  has-patch commit has-dev-note  |     Focuses:  performance
-------------------------------------------+--------------------------

Comment (by vasartam):

 Replying to [comment:89 10upsimon]:
 > The approach we’ve taken will enhance the Scripts API to allow
 developers to declare intended loading strategies for scripts (and inline
 scripts) during registration by replacing the current `$in_footer` param
 with a new `$args` param of type array that supports declaring both
 `in_footer` and `strategy` keys in `wp_register_script` and
 `wp_enqueue_script` functions. This change is fully backward compatible
 with the previous function signatures.

 I claim that these changes are not backward compatible.

 I had my production site broken after automatic update to WordPress 6.3.0
 due to the changes to the `wp_register_script` function API.

 I use PHP 8.0 and I call this function using named parameters:

 {{{#!php
 <?php
 wp_register_script(
         'rk-scripts-vendors',
         THEME_URL . '/dist/vendors.min.js',
         deps: array(),
         ver: filemtime( THEME_DIR . '/dist/vendors.min.js' ),
         in_footer: true,
 );
 }}}

 After automatic update to WordPress 6.3.0 the site started responding with
 the following error:

 {{{
 Unknown named parameter $in_footer in /var/www/rk.loc/wp-
 content/themes/rk/src/features/setup/assets.php:58
 Stack trace:
 #0 /var/www/rk.loc/wp-includes/class-wp-hook.php(310):
 rk_theme_load_assets()
 #1 /var/www/rk.loc/wp-includes/class-wp-hook.php(334):
 WP_Hook->apply_filters()
 #2 /var/www/rk.loc/wp-includes/plugin.php(517): WP_Hook->do_action()
 #3 /var/www/rk.loc/wp-includes/script-loader.php(2225): do_action()
 #4 /var/www/rk.loc/wp-includes/class-wp-hook.php(310):
 wp_enqueue_scripts()
 #5 /var/www/rk.loc/wp-includes/class-wp-hook.php(334):
 WP_Hook->apply_filters()
 #6 /var/www/rk.loc/wp-includes/plugin.php(517): WP_Hook->do_action()
 #7 /var/www/rk.loc/wp-includes/general-template.php(3053): do_action()
 #8 /var/www/rk.loc/wp-content/themes/rk/header.php(14): wp_head()
 #9 /var/www/rk.loc/wp-includes/template.php(785): require_once('...')
 #10 /var/www/rk.loc/wp-includes/template.php(720): load_template()
 #11 /var/www/rk.loc/wp-includes/general-template.php(48):
 locate_template()
 #12 /var/www/rk.loc/wp-content/themes/rk/front-page.php(2): get_header()
 #13 /var/www/rk.loc/wp-includes/template-loader.php(106): include('...')
 #14 /var/www/rk.loc/wp-blog-header.php(19): require_once('...')
 #15 /var/www/rk.loc/index.php(17): require('...')
 #16 {main} thrown in /var/www/rk.loc/wp-
 content/themes/rk/src/features/setup/assets.php on line 58
 }}}

 For changes to be backwards compatible in this case you should have added
 a new argument `$args` before the `$in_footer` argument like so:

 {{{#!php
 <?php
 wp_register_script( $handle, $src, $deps = array(), $ver = false, $args =
 array(), $in_footer = false )
 }}}

 That way the existing code that relies on PHP 8.0 named parameters would
 not be broken.

 Similar API changes have been applied to `wp_enqueue_script` function, and
 it also needs attention. I am not aware of any other significant API
 changes like this one, but maybe you are.

 Hope this encourages to reconsider the choices and release a fix for
 backward compatibility.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/12009#comment:162>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list