Skip to content

Commit ac6e7c9

Browse files
committed
Customize: Preserve CSS cascade for Additional CSS in classic themes.
Restores the `$is_block_theme` check that was inadvertently removed in [61473]. This ensures that for classic themes, the Customizer's Additional CSS continues to be printed separately via `wp_custom_css_cb()` at priority 101 in `wp_head`, preserving its position at the end of the `<head>` for highest CSS specificity. Follow-up to [61473]. Props westonruter. Fixes #64408. git-svn-id: https://develop.svn.wordpress.org/trunk@61479 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8d52517 commit ac6e7c9

2 files changed

Lines changed: 35 additions & 25 deletions

File tree

src/wp-includes/script-loader.php

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2515,35 +2515,43 @@ function wp_enqueue_global_styles() {
25152515
$stylesheet = wp_get_global_stylesheet();
25162516

25172517
/*
2518-
* Dequeue the Customizer's custom CSS
2519-
* and add it before the global styles custom CSS.
2518+
* For block themes, merge Customizer's custom CSS into the global styles stylesheet
2519+
* before the global styles custom CSS, ensuring proper cascade order.
2520+
* For classic themes, let the Customizer CSS print separately via wp_custom_css_cb()
2521+
* at priority 101 in wp_head, preserving its position at the end of the <head>.
25202522
*/
2521-
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
2523+
if ( $is_block_theme ) {
2524+
/*
2525+
* Dequeue the Customizer's custom CSS
2526+
* and add it before the global styles custom CSS.
2527+
*/
2528+
remove_action( 'wp_head', 'wp_custom_css_cb', 101 );
25222529

2523-
/*
2524-
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2525-
* Always do this in Customizer preview for the sake of live preview since it be empty.
2526-
*/
2527-
$custom_css = trim( wp_get_custom_css() );
2528-
if ( $custom_css || is_customize_preview() ) {
2529-
if ( is_customize_preview() ) {
2530-
/*
2531-
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2532-
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2533-
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2534-
* would break live previewing.
2535-
*/
2536-
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2537-
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2538-
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2539-
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2530+
/*
2531+
* Get the custom CSS from the Customizer and add it to the global stylesheet.
2532+
* Always do this in Customizer preview for the sake of live preview since it be empty.
2533+
*/
2534+
$custom_css = trim( wp_get_custom_css() );
2535+
if ( $custom_css || is_customize_preview() ) {
2536+
if ( is_customize_preview() ) {
2537+
/*
2538+
* When in the Customizer preview, wrap the Custom CSS in milestone comments to allow customize-preview.js
2539+
* to locate the CSS to replace for live previewing. Make sure that the milestone comments are omitted from
2540+
* the stored Custom CSS if by chance someone tried to add them, which would be highly unlikely, but it
2541+
* would break live previewing.
2542+
*/
2543+
$before_milestone = '/*BEGIN_CUSTOMIZER_CUSTOM_CSS*/';
2544+
$after_milestone = '/*END_CUSTOMIZER_CUSTOM_CSS*/';
2545+
$custom_css = str_replace( array( $before_milestone, $after_milestone ), '', $custom_css );
2546+
$custom_css = $before_milestone . "\n" . $custom_css . "\n" . $after_milestone;
2547+
}
2548+
$custom_css = "\n" . $custom_css;
25402549
}
2541-
$custom_css = "\n" . $custom_css;
2542-
}
2543-
$stylesheet .= $custom_css;
2550+
$stylesheet .= $custom_css;
25442551

2545-
// Add the global styles custom CSS at the end.
2546-
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
2552+
// Add the global styles custom CSS at the end.
2553+
$stylesheet .= wp_get_global_stylesheet( array( 'custom-css' ) );
2554+
}
25472555

25482556
if ( empty( $stylesheet ) ) {
25492557
return;

tests/phpunit/tests/template.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1561,6 +1561,7 @@ static function () {
15611561
'global-styles-inline-css',
15621562
'normal-css',
15631563
'normal-inline-css',
1564+
'wp-custom-css',
15641565
),
15651566
'BODY' => array(
15661567
'late-css',
@@ -1622,6 +1623,7 @@ function (): void {
16221623
'global-styles-inline-css',
16231624
'normal-css',
16241625
'normal-inline-css',
1626+
'wp-custom-css',
16251627
),
16261628
'BODY' => array(
16271629
'late-css',

0 commit comments

Comments
 (0)