@@ -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 ;
0 commit comments