[wp-trac] [WordPress Trac] #52320: Empty entries in WP_Scripts 'extra' field

WordPress Trac noreply at wordpress.org
Thu Aug 14 10:52:16 UTC 2025


#52320: Empty entries in WP_Scripts 'extra' field
---------------------------+--------------------------------------------
 Reporter:  vanyukov       |       Owner:  (none)
     Type:  defect (bug)   |      Status:  new
 Priority:  normal         |   Milestone:  Awaiting Review
Component:  Script Loader  |     Version:  4.5
 Severity:  trivial        |  Resolution:
 Keywords:  needs-patch    |     Focuses:  performance, coding-standards
---------------------------+--------------------------------------------
Changes (by jonsurrell):

 * keywords:   => needs-patch
 * focuses:   => performance, coding-standards


Comment:

 This is a good spot, I've verified the behavior in trunk (6.8.2 is the
 latest release). It seems that when `wp_add_inline_script()` (or
 `WP_Scripts::add_inline_script()`) is called,
 [https://core.trac.wordpress.org/browser/tags/6.8.2/src/wp-includes/class-
 wp-scripts.php#L473 this line] causes the array to start as `array( false
 )`. `WP_Scripts::get_data()`
 [https://core.trac.wordpress.org/browser/tags/6.8.2/src/wp-includes/class-
 wp-dependencies.php#L313 returns `false` if the script has no associated
 data] and `(array) false` coerces to this array with a `false` element:

 {{{#!php
 <?php
 var_dump( (array) false );
 }}}

 prints

 {{{
 array(1) {
   [0]=>
   bool(false)
 }
 }}}

 It certainly seems like that first `false` element was not intended and
 isn't desirable. It should likely start as an empty array.

 This has been the case since `::add_inline_script()` was added in [36633].

 -----

 One thing to consider, I believe the following could be used to set the
 data to `false`:

 {{{#!php
 <?php
 $wp_scripts->add_data(
   $handle,
   'after', /* or 'before' */
   false
 );
 }}}

 One could argue that coercing to `array( false )` is the correct thing to
 do in that case. However, it seems unlikely that anyone is relying on that
 behavior. A check like this seems reasonable:

 {{{#!php
 <?php
 $data   = $this->get_data( $handle, $position );
 $script = false === $data ? array() : (array) $data;
 }}}

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


More information about the wp-trac mailing list