Conversation
Extends WP_REST_Global_Styles_Controller to support style variations, that is reusable global styles that can be connected to posts, pages, and templates. The controller automatically filters for posts with _wp_style_variation meta and protects the main theme global styles from deletion.
Registers post meta for style variations: - _wp_style_variation: Identifies wp_global_styles posts as style variations - _wp_connected_style_variation: Links posts/pages/templates to a style variation Includes functions for retrieving style variation data and a filter to pass the connected style variation ID to the block editor settings. Also adds frontend CSS enqueuing for posts and pages with connected style variations.
Adds reducer, action, and selector for tracking which style variation is currently selected for editing in the Styles sidebar. The value is 0 when editing main global styles, or a style variation post ID otherwise.
Implements StyleVariationSelector dropdown component for selecting which style variation to edit in the Styles sidebar. Includes useStyleVariations hook using entities, a StyleVariationSelector dropdown with "create new" modal, and updates global styles hooks to accept styleVariationId parameter.
- StyleVariationSelector dropdown in Styles sidebar header - PostStyleVariation panel for connecting posts to variations - GlobalStylesProvider merges connected variation styles - StyleBook and canvas support for previewing variations - Site editor sidebar integration
|
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
|
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @mtias! In case you missed it, we'd love to have you join us in our Slack community. If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information. |
|
Size Change: +2.88 kB (+0.11%) Total Size: 2.58 MB
ℹ️ View Unchanged
|
tyxla
left a comment
There was a problem hiding this comment.
This is a great start, and an exciting way to add persistent customizable sets of styles and assign them in different contexts. Very very cool!
| // Only run on singular pages. | ||
| if ( ! is_singular() ) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
| ); | ||
|
|
||
| // Meta to connect posts/pages/templates to a style variation. | ||
| $post_types = array( 'post', 'page', 'wp_template', 'wp_template_part', 'wp_block' ); |
There was a problem hiding this comment.
Do we intend to have this per template part and per block? If yes, how do those style variations stack, and do we have a defined hierarchy?
| * @return int The connected style variation ID, or 0 if none. | ||
| */ | ||
| function gutenberg_get_connected_style_variation_id( $post_id ) { | ||
| return (int) get_post_meta( $post_id, GUTENBERG_CONNECTED_STYLE_VARIATION_META_KEY, true ); |
There was a problem hiding this comment.
Should we ensure it exists before converting it to (int)? A non-existing meta will be false and will be converted to 0.
| return array( | ||
| 'id' => $post->ID, | ||
| 'title' => $post->post_title, | ||
| 'content' => json_decode( $post->post_content, true ), |
There was a problem hiding this comment.
Some sanitization might be necessary here
| } | ||
|
|
||
| // Get the connected style variation for this post. | ||
| $connected_style_variation_id = gutenberg_get_connected_style_variation_id( $post_id ); |
There was a problem hiding this comment.
Should we check if there is any before assigning it below?
| return new WP_Error( | ||
| 'rest_cannot_delete', | ||
| __( 'The variation cannot be deleted.', 'gutenberg' ), | ||
| array( 'status' => 500 ) |
There was a problem hiding this comment.
A 500 doesn't make much sense here. We could use 403, 404, or even 409. Might be nice to have a more specific mapping as this error comes directly from wpdb IIRC.
| > | ||
| { selectedStyleVariationInfo && ( | ||
| <p className="edit-site-styles__style-variation-info"> | ||
| { `Editing "${ selectedStyleVariationInfo.title }" style variation. Changes apply only when this style is connected to a post, page, template, or pattern.` } |
| <MenuGroup label={ __( 'Style Variations' ) }> | ||
| <MenuItem | ||
| role="menuitemradio" | ||
| isSelected={ selectedStyleVariation === 0 } |
There was a problem hiding this comment.
| isSelected={ selectedStyleVariation === 0 } | |
| isSelected={ ! isStyleVariationSelected } |
| role="menuitemradio" | ||
| isSelected={ selectedStyleVariation === 0 } | ||
| icon={ | ||
| selectedStyleVariation === 0 ? check : null |
There was a problem hiding this comment.
| selectedStyleVariation === 0 ? check : null | |
| ! isStyleVariationSelected ? check : null |
| onClose(); | ||
| } } | ||
| > | ||
| { __( 'Create new style variation' ) } |
There was a problem hiding this comment.
| { __( 'Create new style variation' ) } | |
| { __( 'Create a new style variation' ) } |
|
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 Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @critterverse. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
ramonjd
left a comment
There was a problem hiding this comment.
I didn't get a lot of time to test, but thanks for exploring this.
I know this is a demo/POC so take all my comments/questions with sacks of salt. The general direction is 💯
There are a lot of folks from this issue:
who I'm sure will have valuable feedback to share if this ramps up.
🍺 🍺 🍺 🍺 🍺 🍺
| * Retrieves a collection of style variations. | ||
| * | ||
| * Returns all wp_global_styles posts that are style variations | ||
| * (identified by _wp_style_variation meta). |
There was a problem hiding this comment.
Is this the same value as the PHP const below? Does that mean it should be configurable?
I'm looking at:
define( 'GUTENBERG_STYLE_VARIATION_META_KEY', '_wp_style_variation' );
| 'meta_query' => array( | ||
| array( | ||
| 'key' => '_wp_style_variation', | ||
| 'value' => '1', | ||
| ), | ||
| ), |
There was a problem hiding this comment.
Not sure, but does WP_Theme_JSON_Resolver_Gutenberg ::get_user_data_from_wp_global_styles() needs a similar meta_query to exclude these style variations?
|
|
||
| // Meta to connect posts/pages/templates to a style variation. | ||
| $post_types = array( 'post', 'page', 'wp_template', 'wp_template_part', 'wp_block' ); | ||
| foreach ( $post_types as $post_type ) { |
There was a problem hiding this comment.
Wondering if you see this happening when registering posts over in https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/post.php#L23
| */ | ||
| function gutenberg_enqueue_style_variation_css() { | ||
| // Only run on singular pages. | ||
| if ( ! is_singular() ) { |
There was a problem hiding this comment.
Similar to below
I was wondering if style variations would apply site wide one day. E.g, a global styles post could be promoted from a style variation to become the main global styles post. Or is the per-post-only model the intended long-term design?
I think @youknowriad's issue is relevant.
If yes to alternative "style variations" for themes, then this could blur the distinction between root/globalStyles and postType/wp_global_styles in the entity system.
Currently, both use the same REST endpoint (/wp/v2/global-styles/{id}) but are tracked as different entities
Once again, I don't have the full picture here.
| const [ styles, settings ] = useGlobalStylesOutputWithConfig( | ||
| mergedConfig, | ||
| disableRootPadding | ||
| ); |
There was a problem hiding this comment.
This probably is going to happen anyway, but it'd good to be able to preview how the styles look in the preview window while creating them.
If it's already working in this PR, then ignore me! Maybe just my setup.
|
I'll pick this up again after 7.0 |

WIP: Not to merge!
Closes #39281, #40318 and #53480; See #59404
This work implements style variations as objects of wp_global_styles.
Style Variations allow users to create named sets of global styles and connect them to specific posts, pages, templates, or patterns. When a post type is connected to a variation, it renders with that variation's styles instead of the main global styles.
style-variations.mp4
The variations are managed through the existing Styles interface in the Site Editor, and the styles are applied using the same global styles merging system. On the frontend, connected content receives the variation's CSS; in the editor, the canvas reflects the connected variation's styles during editing.
Management
Users can create new variations and they are customized using the style book.
Variations can be named:
Connection
Posts can be assigned to a style variation in the post inspector.