[wp-trac] [WordPress Trac] #51124: Can we get an additional parameter in wp_add_inline_script to set the script type?

WordPress Trac noreply at wordpress.org
Sat Oct 2 00:06:03 UTC 2021


#51124: Can we get an additional parameter in wp_add_inline_script to set the
script type?
-------------------------------------------------+-------------------------
 Reporter:  hcabrera                             |       Owner:  audrasjb
     Type:  feature request                      |      Status:  accepted
 Priority:  normal                               |   Milestone:  5.9
Component:  Script Loader                        |     Version:
 Severity:  normal                               |  Resolution:
 Keywords:  good-first-bug needs-patch has-      |     Focuses:
  unit-tests                                     |
-------------------------------------------------+-------------------------
Changes (by azaozz):

 * keywords:  good-first-bug has-patch => good-first-bug needs-patch has-
     unit-tests


Comment:

 Looking at the patches, it's not going to be that easy :)

 To add support for `type` attribute to each inline script the requirements
 are:

 1. The attribute has to be set only for that script tag. With the current
 patches it is set "globally" so everything that uses `$this->type_attr` in
 WP_Scripts will use the new/special type. A solution would be to add
 another `data` on the script handle holding the type.

 2. There can be more than one inline script for each "script handle". To
 support custom types, each inline script should be able to have it's own
 type (if set), or fall back to the default, global `$this->type_attr`. So
 the above mentioned `data` on the script handle should be an array
 corresponding to the number of inline scripts that have custom types set.

 3. As this is getting pretty complex, unit tests are a must.

 I've looked a bit at how this may work and there's another "gotcha",
 unfortunately. The `public function print_inline_script( $handle,
 $position = 'after', $echo = true )` returns the concatenated inline
 script sources. Not sure what to do if one of the inline scripts has a
 custom type. Seems it will have to be excluded from the concatenated
 scripts, as it is not JavaScript, but then it gets "lost" if a function
 expects to use the return value (`$echo` is false). Any ideas on how to
 handle that case?

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


More information about the wp-trac mailing list