Make WordPress Core


Ignore:
Timestamp:
10/07/2025 11:32:04 PM (3 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/twentytwentyone/functions.php

    r56556 r60913  
    631631 */
    632632function twentytwentyone_add_ie_class() {
    633     ?>
    634     <script>
    635     if ( -1 !== navigator.userAgent.indexOf( 'MSIE' ) || -1 !== navigator.appVersion.indexOf( 'Trident/' ) ) {
    636         document.body.classList.add( 'is-IE' );
    637     }
    638     </script>
    639     <?php
     633    $script  = "
     634        if ( -1 !== navigator.userAgent.indexOf('MSIE') || -1 !== navigator.appVersion.indexOf('Trident/') ) {
     635            document.body.classList.add('is-IE');
     636        }
     637    ";
     638    $script .= '//# sourceURL=' . rawurlencode( __FUNCTION__ );
     639
     640    if ( function_exists( 'wp_print_inline_script_tag' ) ) {
     641        wp_print_inline_script_tag( $script );
     642    } else {
     643        echo "<script>$script</script>\n";
     644    }
    640645}
    641646add_action( 'wp_footer', 'twentytwentyone_add_ie_class' );
Note: See TracChangeset for help on using the changeset viewer.