[wp-trac] [WordPress Trac] #43248: wp_localize_script() and ultimately print_extra_script() not using script_loader_tag filter

WordPress Trac noreply at wordpress.org
Wed Feb 7 14:45:45 UTC 2018


#43248: wp_localize_script() and ultimately print_extra_script() not using
script_loader_tag filter
---------------------------------------+-----------------------------
 Reporter:  csmillie                   |      Owner:
     Type:  enhancement                |     Status:  new
 Priority:  normal                     |  Milestone:  Awaiting Review
Component:  I18N                       |    Version:  4.9.4
 Severity:  normal                     |   Keywords:
  Focuses:  accessibility, javascript  |
---------------------------------------+-----------------------------
 When you add a localized value like:

 {{{#!php
 <?php
     wp_localize_script( "scripts", "localized", 'value' );
 }}}

 Generates

 {{{
 #!html
 <script type="text/javascript">
 /* <![CDATA[ */
 var php_vars = "value";
 /* ]]> */
 </script>
 }}}

 This value is added to wp_script->localized and then outputted using
 print_extra_script() which directly echo's the code without using the
 script_loader_tag filter.

 I'd suggest adjusting print_extra_script() to use the script_loader_tag
 filter.   I'm unclear if a new filter should be created but I think the
 existing was added to filter code like the localize additions.

 I created a patch and I'm open to contributing if it's acceptable:

 {{{#!php
 <?php
 diff --git a/wp-includes/class.wp-scripts.php b/wp-includes/class.wp-
 scripts.php
 index a9299f0..b770734 100644
 --- a/wp-includes/class.wp-scripts.php
 +++ b/wp-includes/class.wp-scripts.php
 @@ -202,13 +202,25 @@ class WP_Scripts extends WP_Dependencies {
                 if ( !$echo )
                         return $output;

 -               echo "<script type='text/javascript'>\n"; // CDATA and
 type='text/javascript' is not needed for HTML 5
 -               echo "/* <![CDATA[ */\n";
 -               echo "$output\n";
 -               echo "/* ]]> */\n";
 -               echo "</script>\n";
 +               $tag = "<script type='text/javascript'>\n"; // CDATA and
 type='text/javascript' is not needed for HTML 5
 +               $tag .= "/* <![CDATA[ */\n";
 +               $tag .= "$output\n";
 +               $tag .= "/* ]]> */\n";
 +               $tag .= "</script>\n";
 +
 +               /**
 +                 * Filters the HTML script tag of an enqueued script.
 +                 *
 +                 * @since 4.1.0
 +                 *
 +                 * @param string $tag    The `<script>` tag for the
 enqueued script.
 +                 * @param string $handle The script's registered handle.
 +                 * @param string $src    The script's source URL.
 +                 */
 +                $tag = apply_filters( 'script_loader_tag', $tag, $handle,
 $output );
 +
 +               echo $tag;

 -               return true;
         }

         /**
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43248>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list