[wp-trac] [WordPress Trac] #22249: Add ability to set or remove attributes on enqueued scripts and styles.

WordPress Trac noreply at wordpress.org
Mon Mar 5 18:30:39 UTC 2018


#22249: Add ability to set or remove attributes on enqueued scripts and styles.
-------------------------------------------+-----------------------------
 Reporter:  ryanve                         |       Owner:
     Type:  enhancement                    |      Status:  assigned
 Priority:  normal                         |   Milestone:  Future Release
Component:  Script Loader                  |     Version:
 Severity:  normal                         |  Resolution:
 Keywords:  early has-patch needs-testing  |     Focuses:
-------------------------------------------+-----------------------------

Comment (by mor10):

 `async` and `defer` have unique roles in the context of the script tag and
 should be handled separately from other attributes.
 [https://developers.google.com/speed/docs/insights/BlockingJS More
 details]. As mentioned in
 [https://core.trac.wordpress.org/ticket/12009#comment:1 12009], there are
 three possible script loading conditions: nothing (default), `async`, and
 `defer`. If `async` or `defer` are used, they are added to the script tag
 as booleans without values:


 {{{
 <script async src="javascript.js"></script>
 }}}

 IMO these script loading attributes are significant enough to warrant
 their own parameter in the affected functions. Something like this:

 {{{#!php
 <?php
 wp_enqueue_script( 'label',
                    get_theme_file_uri( 'script.js' ),
                    array(),
                    '1.0',
                    false,
                    defer,
                    array( 'data-attribute' => 'value' )
 );
 }}}

 which would output the following code in `<head>`:


 {{{
 <script type='text/javascript' src='[...]/script.js?ver=1.0' defer data-
 attribute='value'></script>
 }}}

 The new property could either be blank (not filled in) or contain the word
 `async` or the word `defer`. Alternatively it could be captured using some
 sort of attribute/value pair like `script-loading="async"` or similar. The
 main point is separation of concerns: `async` / `defer` are kept as stand-
 alone parameters and outputs in the overall function.

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


More information about the wp-trac mailing list