Skip to content

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jun 14, 2025

The implementation here is described in https://core.trac.wordpress.org/ticket/63589#comment:23

Instead of re-introducing the separate STYLE tag for the Customizer's Custom CSS in the Customizer Preview just for the sake of live preview, in my new approach I'm surrounding the Customizer's Custom CSS in CSS comments. These are used to locate where the Customizer's Custom CSS appears in the overall global styles so that the Customizer Preview JS logic can surgically insert the updates into the existing inline stylesheet.

This will ensure that the Custom CSS in the Customizer Preview will look exactly the same as the frontend, since the CSS cascade will be the same.

The new approach is now part of WordPress/wordpress-develop#9000

Obsolete Approach

Adding the Additional CSS from the Customizer to the global styles makes sense, except not in the Customizer preview. This is because wp-includes/js/customize-preview.js is supposed to dynamically update the STYLE#wp-custom-css element with the new stylesheet whenever it changes. So this PR prevents putting the Additional CSS into the global styles when in the Customizer preview context.

In the Customizer preview, this PR prevents injecting the Customizer's CSS into the global-styles in between the global stylesheet and the Custom CSS. Instead, it prints the Customizer's Custom CSS before printing other styles. This ensures that the CSS cascade is preserved for global styles. However, normally the Customizer's CSS is printed at wp_head priority 101, long after the wp_print_styles() runs at priority 8. This was to ensure that the Customizer's Custom CSS would be able to override any and all styles added by the theme or plugins; however, in a block theme the Customizer's Custom CSS is already being deprioritized.

See the diff for how * { color: blue; } in the Customizer and * { color: red; } in global styles Custom CSS appear in the Customizer preview and outside in the normal view:

--- frontend.prettier.html	2025-07-07 18:26:17
+++ customizer-preview.prettier.html	2025-07-07 18:26:08
@@ -539,6 +542,11 @@
 					});
 			})(window, document, window._wpemojiSettings);
 		</script>
+		<style id="wp-custom-css">
+			* {
+				color: blue;
+			}
+		</style>
 		<link
 			rel="stylesheet"
 			id="dashicons-css"
@@ -2284,9 +2298,6 @@
 				font-family: var(
 					--wp--preset--font-family--fira-code
 				) !important;
 			}
-			* {
-				color: blue;
-			}
 			* {
 				color: red;

This issue was introduced in #58991

Screencast

Before

Screen.Recording.2025-06-14.at.13.28.10.mov

After

Screen.Recording.2025-06-14.at.13.31.21.mov

@westonruter westonruter requested review from tellthemachines and removed request for spacedmonkey June 14, 2025 20:44
Copy link

github-actions bot commented Jun 14, 2025

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@westonruter westonruter added [Type] Bug An existing feature does not function as intended Needs PHP backport Needs PHP backport to Core CSS Styling Related to editor and front end styles, CSS-specific issues. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Jun 14, 2025
westonruter added a commit to westonruter/wordpress-develop that referenced this pull request Jun 18, 2025
@westonruter westonruter force-pushed the fix/custom-css-customize-preview branch from 617ca2d to 259b5fc Compare June 18, 2025 00:53
Copy link

github-actions bot commented Jul 8, 2025

Flaky tests detected in 37846e0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/16613818055
📝 Reported issues:

@westonruter westonruter marked this pull request as draft July 28, 2025 04:45
@westonruter
Copy link
Member Author

I've marked this as a draft now because the core PR now has a better approach, as described in https://core.trac.wordpress.org/ticket/63589#comment:23

@westonruter westonruter marked this pull request as ready for review July 28, 2025 05:04
*
* Note: The logic in this function would be back-ported into customize-preview.js.
*/
function gutenberg_add_customizer_block_theme_custom_css_preview_js() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If WordPress/wordpress-develop#9000 will be shipped with WP 6.9, can we move this code into the lib/compat/wordpress-6.9 directory? Once the minimum supported version of WordPress for Gutenberg increases to 6.9, this code will no longer be necessary and can be removed.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about 37846e0?

sprintf( '( %s )( %s )', $js_function, wp_json_encode( $setting_id ) )
);
}
add_action( 'wp_enqueue_scripts', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't tested it to see if it works, but can we use the customize_preview_init hook to avoid unnecessary function execution on the frontend? This hook seems to be widely used in default themes to enqueue scripts as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So like this?

add_action( 'customize_preview_init', static function () {
    add_action( 'wp_enqueue_scripts', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
} );

This would then eliminate the need for the is_customize_preview() check in the function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, there's not really any need to use wp_enqueue_scripts here. We're just adding an inline script to a script that already registered. So this can be:

Suggested change
add_action( 'wp_enqueue_scripts', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
add_action( 'customize_preview_init', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See d9f7988

westonruter and others added 4 commits July 29, 2025 21:25
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
@westonruter westonruter requested a review from t-hamano July 30, 2025 05:00
Copy link
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR works correctly on both WordPress 6.7, which the Gutenberg plugin supports, and the latest core trunk.


if ( version_compare( get_bloginfo( 'version' ), '6.9.0', '<' ) ) {
add_action( 'customize_preview_init', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This conditional statement will return true if WordPress is in Alpha or Beta, or RC, so the action hook will run even if the JS logic has already been merged into core.

Finally, I would suggest one of the following changes:

  1. Remove the conditional statement because there is a skip process here.
  2. Use is_wp_version_compatible(), which allows us to compare with he pure version number:
if ( is_wp_version_compatible( '6.9' ) ) {
	add_action( 'customize_preview_init', 'gutenberg_add_customizer_block_theme_custom_css_preview_js' );
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@t-hamano I included both on purpose actually… I wanted to avoid adding that additional JS when in 6.9 stable. But for 6.9-alpha, I thought it was fine to include it for the sake of devs on different nightly versions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, if that's your intention, I think the code is fine as is 👍

@t-hamano t-hamano removed the Needs PHP backport Needs PHP backport to Core label Jul 30, 2025
@westonruter westonruter changed the title Fix ability to preview Additional CSS changes in the Customizer Fix ability to preview Additional CSS changes in the Customizer when using a Block Theme Jul 30, 2025
@westonruter westonruter merged commit 9b3d784 into trunk Jul 30, 2025
68 of 72 checks passed
@westonruter westonruter deleted the fix/custom-css-customize-preview branch July 30, 2025 16:11
@github-actions github-actions bot added this to the Gutenberg 21.4 milestone Jul 30, 2025
peterwilsoncc pushed a commit to peterwilsoncc/gutenberg-build that referenced this pull request Jul 30, 2025
…using a Block Theme (#70428)

* Fix ability to preview Custom CSS changes in the Customizer

* Add backport changelog

WordPress/wordpress-develop#9000
WordPress/gutenberg#70428

* Undo unnecessary update to deprecated function

* Ensure global styles are shown in Customizer preview and override Customizer styles in cascade

* Add Customizer details to the comment

* Update Customizer Custom CSS inside of global styles for live preview

* Make sure saved milestone comments are removed from stored CSS when previewing

* Add line breaks before/after Custom CSS for readability/debugging

Co-authored-by: Peter Wilson <peterwilsoncc@git.wordpress.org>

* Remove erroneous duplicated appending of global styles custom CSS

* Simplify adding inline script to customize-preview

Co-authored-by: t-hamano <wildworks@git.wordpress.org>

* Improve JS formatting

* Add commit reference

* Move custom CSS preview logic to 6.9 compat include

Co-authored-by: t-hamano <wildworks@git.wordpress.org>

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Peter Wilson <peterwilsoncc@git.wordpress.org>

Source: WordPress/gutenberg@9b3d784
adamsilverstein pushed a commit to adamsilverstein/gutenberg that referenced this pull request Jul 31, 2025
…using a Block Theme (WordPress#70428)

* Fix ability to preview Custom CSS changes in the Customizer

* Add backport changelog

WordPress/wordpress-develop#9000
WordPress#70428

* Undo unnecessary update to deprecated function

* Ensure global styles are shown in Customizer preview and override Customizer styles in cascade

* Add Customizer details to the comment

* Update Customizer Custom CSS inside of global styles for live preview

* Make sure saved milestone comments are removed from stored CSS when previewing

* Add line breaks before/after Custom CSS for readability/debugging

Co-authored-by: Peter Wilson <peterwilsoncc@git.wordpress.org>

* Remove erroneous duplicated appending of global styles custom CSS

* Simplify adding inline script to customize-preview

Co-authored-by: t-hamano <wildworks@git.wordpress.org>

* Improve JS formatting

* Add commit reference

* Move custom CSS preview logic to 6.9 compat include

Co-authored-by: t-hamano <wildworks@git.wordpress.org>

---------

Co-authored-by: westonruter <westonruter@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: Peter Wilson <peterwilsoncc@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CSS Styling Related to editor and front end styles, CSS-specific issues. Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants