Template Style Variations (Exploration/POC)#75330
Conversation
|
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. |
|
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 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. |
…for template style variations
Introduces the foundational backend infrastructure for template style
variations — an experimental feature that allows assigning distinct
global-style configurations to individual templates.
New registries:
- WP_Style_Variations_Registry_Gutenberg (singleton) for style variations
- WP_Base_Themes_Registry_Gutenberg (singleton) for base themes
Public PHP API (lib/template-style-variations.php):
- Register/unregister style variations and base themes
- Get/set variation assignments for templates
- Manage variation-specific wp_global_styles posts
REST endpoints:
- GET /wp/v2/style-variations — list registered variations
- GET /wp/v2/base-themes — list registered base themes
- POST /wp/v2/templates-variation-post/{id} — create/get variation posts
Gated behind the gutenberg-template-style-variations experiment flag.
Connects the JavaScript editor to the PHP REST API and makes the global styles system template-variation-aware. core-data changes: - New actions, reducers, resolvers for fetching registered style variations and base themes from REST API - New selectors: __experimentalGetRegisteredStyleVariations, __experimentalGetBaseThemes editor changes: - TemplateStyleVariationContext + Provider — React context that tracks the current template's variation ID, post ID, and base theme - useGlobalStyles() now reads the variation context to serve variation-specific styles and edits instead of global ones - GlobalStylesProvider wraps children in TemplateStyleVariationProvider - GlobalStylesRenderer integrates with template-aware styles - Exports useGlobalStyles as private API
9dd3143 to
38246ac
Compare
tyxla
left a comment
There was a problem hiding this comment.
Thanks @costasovo, this looks useful and quite a frequently requested feature (see more details here).
As it's very late in the 7.0 cycle, this has a minimal chance to land on time, so it will likely need to be slated for as early as 7.1.
Still, I think it's worth broader feedback: cc @WordPress/gutenberg-core
User-facing UI for selecting and previewing template style variations. New components: - TemplateStyleVariationPicker — grid of visual previews for each registered variation, fetches post data for user edits, allows selecting a variation for the current template - TemplateStyleVariationPanel — sidebar panel shown when editing templates in the site editor Modified: - Template list fields now use usePatternSettingsWithVariation() to render variation-aware previews in the template listing - global-styles-ui exports TemplateStyleVariationPicker
Makes the editor canvas render using the template's assigned variation styles. This is needed because useSpecificEditorSettings() runs outside the TemplateStyleVariationProvider context. New: - TemplateStyleVariationProvider wrapper for the site editor Modified: - use-site-editor-settings.js — useTemplateStyleVariationLookup() reads post data when available, falls back to registered data; mergeGlobalStyles() overlays variation styles onto editor settings - editor/index.js — integrates TemplateStyleVariationProvider and passes resolvedTemplateId
PHP that generates the correct stylesheet on the actual site (not the editor) for templates that have an assigned style variation. Modified: - WP_Theme_JSON_Resolver_Gutenberg — new methods: get_merged_data_for_template() (core merge method), get_theme_data_with_base(), get_user_data_from_post(), get_user_data_from_registered_variation() - global-styles-and-settings.php — gutenberg_get_global_stylesheet_for_template() - script-loader.php — gutenberg_enqueue_global_styles() now uses template-specific stylesheet when a variation is assigned
Example data for testing the template style variations feature. Only loaded when the gutenberg-template-style-variations experiment flag is enabled. Registers 2 base themes (minimal, dark-base) and 5 style variations (dark-mode, warm-tones, cool-tones, high-contrast, pastel-colors).
38246ac to
e919100
Compare
|
I think @mtias has explored something similar in a previous PR. Might be good to check approaches and ideas :) |
|
Yep, that's the PR in question: #73978 |
|
Yes, it's very similar, but the architectures differ a bit. It'd be good to start breaking it down into components. |
What?
Exploratory prototype for template-specific style variations — allowing individual templates to use distinct global style configurations instead of inheriting a single site-wide set of styles.
This is a proof of concept and is not intended to be merged as-is. It is meant to validate the architecture and inform a proposal (see #75331).
Note: The style variation picker in the template sidebar is a placeholder UI for demonstration purposes. A more natural home for this would likely be the existing style browser in the Styles panel, where a user could choose whether to apply a style variation site-wide or scope it to the current template.
Why?
The current global styles system operates site-wide: every template inherits the same
theme.jsonsettings and user customizations. This proposal makes it easier to support real-world scenarios like:How?
The feature is gated behind the
gutenberg-template-style-variationsexperiment flag (Gutenberg > Experiments). Nothing is loaded or registered unless the experiment is enabled.Architecture
Data model — three entities and their relationships:
Style Variations — registered sets of
theme.json-compatible styles/settings (similar to theme style variations, but scoped to templates). Stored in aWP_Style_Variations_Registrysingleton; plugins and themes register them viagutenberg_register_style_variation().Base Themes — optional alternative
theme.jsonconfigurations that replace the active theme's base layer. Registered viagutenberg_register_base_theme(). A style variation can reference a base theme to start from a completely different design foundation.Templates — link to a style variation by ID via post meta (
_wp_style_variation_idonwp_template). Multiple templates can share the same variation.How variations become editable:
When a user edits a variation's styles in the Styles UI, a
wp_global_stylespost is created for that variation (linked via_wp_source_variation_idmeta). This post stores user customizations scoped to the variation, parallel to how the defaultwp_global_stylespost stores site-wide customizations. The relationship is one-to-one: one post per variation, shared by all templates using it.Style merge order (matching the existing global styles cascade):
REST API for third-party integration:
GET /wp/v2/style-variationsGET /wp/v2/base-themesPATCH /wp/v2/templates/{id}style_variationon a templatePOST /wp/v2/templates-variation-post/{id}wp_global_stylespost for a variationPHP registration API for plugins/themes:
Demo
The PR includes demo code (
lib/experimental/demo-template-style-variations.php) that exercises the registration API — it registers two base themes and five style variations, and creates a "Campaign Landing Page" template with an assigned variation. This serves as a practical reference for how plugins and themes would usegutenberg_register_style_variation()andgutenberg_register_base_theme().Testing Instructions
Screenshots or screencast
template_styles.mp4