Make WordPress Core


Ignore:
Timestamp:
10/07/2025 11:32:04 PM (12 months ago)
Author:
westonruter
Message:

Bundled Themes: Use wp_print_inline_script_tag() when available and include sourceURL for JS.

Instead of manually constructing the markup for SCRIPT tags, leverage wp_print_inline_script_tag() when available to do the construction while also ensuring filters may inject additional attributes on the SCRIPT tags, such as nonce for CSP. When the function is not available (prior to WP 5.7), fall back to the manual markup construction.

This also adds the sourceURL comments to the inline scripts to facilitate debugging, per #63887.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/9416.

Follow-up to [60909], [60899].

Props debarghyabanerjee, westonruter, hbhalodia, peterwilsoncc, sabernhardt, poena.
See #63887, #59446.
Fixes #63806.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentysixteen/inc/customizer.php

    r60527 r60913  
    839839 *
    840840 * @since Twenty Sixteen 1.0
     841 * @since Twenty Sixteen 4.1 Added `wp_print_inline_script_tag()` support.
    841842 */
    842843function twentysixteen_color_scheme_css_template() {
    … …  
    849850                'border_color'          => '{{ data.border_color }}',
    850851        );
    851         ?>
    852         <script type="text/html" id="tmpl-twentysixteen-color-scheme">
    853                 <?php echo twentysixteen_get_color_scheme_css( $colors ); ?>
    854         </script>
    855         <?php
     852
     853        $css_template = twentysixteen_get_color_scheme_css( $colors );
     854
     855        if ( function_exists( 'wp_print_inline_script_tag' ) ) {
     856                wp_print_inline_script_tag(
     857                        $css_template,
     858                        array(
     859                                'type' => 'text/html',
     860                                'id'   => 'tmpl-twentysixteen-color-scheme',
     861                        )
     862                );
     863        } else {
     864                echo '<script type="text/html" id="tmpl-twentysixteen-color-scheme">' . $css_template . '</script>';
     865        }
    856866}
    857867add_action( 'customize_controls_print_footer_scripts', 'twentysixteen_color_scheme_css_template' );
Note: See TracChangeset for help on using the changeset viewer.