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/twentytwenty/inc/template-tags.php

    r60536 r60913  
    661661 */
    662662function twentytwenty_no_js_class() {
    663 
    664         ?>
    665         <script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
    666         <?php
     663        $js  = "document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );";
     664        $js .= "\n//# sourceURL=" . rawurlencode( __FUNCTION__ );
     665
     666        if ( function_exists( 'wp_print_inline_script_tag' ) ) {
     667                wp_print_inline_script_tag( $js );
     668        } else {
     669                echo "<script>$js</script>\n";
     670        }
    667671}
    668672
Note: See TracChangeset for help on using the changeset viewer.