. */ function gutenberg_add_customizer_block_theme_custom_css_preview_js() { if ( ! wp_is_block_theme() ) { return; } $setting_id = 'custom_css[' . get_stylesheet() . ']'; $js_function = << { wp.customize.bind( 'preview-ready', () => { // Skip running logic that is already merged in trunk. if ( window._wpCustomizeSettings.theme.isBlockTheme ) { return; } wp.customize( settingId, function ( setting ) { setting.bind( function ( newValue ) { const style = document.querySelector( 'style#global-styles-inline-css' ); if ( ! style ) { return; } // Forbid milestone comments from appearing in Custom CSS which would break live preview. newValue = newValue.replace( /\/\*(BEGIN|END)_CUSTOMIZER_CUSTOM_CSS\*\//g, '' ); style.textContent = style.textContent.replace( /(\/\*BEGIN_CUSTOMIZER_CUSTOM_CSS\*\/)((?:.|\s)*?)(\/\*END_CUSTOMIZER_CUSTOM_CSS\*\/)/, function ( match, beforeComment, oldValue, afterComment ) { return beforeComment + newValue + afterComment; } ); } ); } ); } ); } JS; wp_add_inline_script( 'customize-preview', sprintf( '( %s )( %s )', $js_function, wp_json_encode( $setting_id, JSON_HEX_TAG | JSON_UNESCAPED_SLASHES ) ) ); } if ( version_compare( get_bloginfo( 'version' ), '6.9.0', '<' ) ) { add_action( 'customize_preview_init', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' ); }