Skip to content

Fix: preserve parent presets when saving child theme changes - #893

Open
CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/child-theme-preset-loss
Open

CookieDarb wants to merge 1 commit into
WordPress:trunkfrom
CookieDarb:fix/child-theme-preset-loss

Conversation

@CookieDarb

Copy link
Copy Markdown

What?

Closes #813

Fixes an issue where saving changes to a child theme in the Site Editor completely wipes out the parent theme's color palette (and other indexed presets like gradients or font sizes).

Why?

When saving changes to a child theme, Create Block Theme exports its theme.json in 'current' mode so the child theme only writes its deltas rather than cloning the parent theme.

While WordPress Core recursively merges associative dictionaries (e.g., layout and block style overrides), Core treats indexed preset arrays (settings.color.palette, settings.typography.fontSizes, etc.) as complete replacements. When a user adds or modifies a single color in a child theme, CBT previously wrote only that user's entries into the child's theme.json, leading Core to drop all parent theme presets at runtime.

How?

  • includes/create-theme/resolver_additions.php:
    • In CBT_Theme_JSON_Resolver::export_theme_data(), when exporting in 'current' mode and the active theme is a child theme, read the raw parent theme.json and backfill parent preset entries via merge_parent_preset_arrays().
    • Added helper merge_parent_preset_arrays(): recursively traverses the child data. For any slug-keyed preset lists, it merges parent entries with child entries. Child overrides replace parent entries by slug in-place, new child presets are appended, and non-preset objects (layout, block styles) remain untouched child deltas.
    • Added is_preset_list() and is_list() to safely identify slug-keyed preset arrays while supporting PHP 7.4+.
  • tests/test-resolver-preset-merge.php:
    • Added unit test coverage for slug-based preset merging, intentional clear (palette: []), and non-preset delta preservation.

Testing Instructions

  1. Activate a child theme that inherits a parent theme with a defined palette (e.g., a child of Twenty Twenty-Four or Twenty Twenty-Five).
  2. Go to Appearance > Editor > Styles > Colors > Palette.
  3. Add a new custom color (or edit an existing theme color) and save changes via Create Block Theme (Save Changes).
  4. Inspect the child theme's theme.json on disk or refresh the Site Editor.
  5. Verify that parent theme colors remain available alongside the new custom color.

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini
Used for: Adding tests, documentation and aligning implementation.

@CookieDarb
CookieDarb marked this pull request as ready for review October 21, 2026 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue with colors after "save changes to theme"

1 participant