Skip to content

fix: save unedited theme patterns in templates as pattern references - #910

Open
ishitaj34 wants to merge 2 commits into
WordPress:trunkfrom
ishitaj34:fix/issue-894
Open

ishitaj34 wants to merge 2 commits into
WordPress:trunkfrom
ishitaj34:fix/issue-894

Conversation

@ishitaj34

Copy link
Copy Markdown

What?

Closes #894

Saves unedited theme patterns in templates as <!-- wp:pattern {"slug":"..."} /--> references instead of copying their full inlined block markup into template files.

Why?

When a theme pattern is inserted into a template in the Site Editor, Gutenberg inlines its full block structure and marks the root block with metadata.patternName.

Saving the template with Create Block Theme previously wrote those inlined copies directly into the theme's template files. The template's copy and the standalone pattern file start differing and bug fixes or styling updates made to the pattern never reach the inlined copies in templates.

Core themes build templates using pattern references (<!-- wp:pattern {"slug":"theme/pattern"} /-->). Saving unedited patterns as references preserves this design, keeps template files clean, and ensures future pattern updates automatically propagate to templates.

How?

In includes/create-theme/theme-templates.php:

  • Added replace_unedited_theme_patterns() to swap matching unedited blocks for <!-- wp:pattern --> references before localization and export.
  • Added is_block_matching_pattern() and are_blocks_equivalent() to compare blocks against registered active/parent theme patterns, expanding nested pattern references and normalizing editor metadata and common attribute migrations.
  • Added cbt_replace_with_pattern_reference filter to allow opting blocks out of replacement.
  • Added preservePatternSlugs support to replace_template_namespace() so child themes keep references to parent patterns.

In includes/create-theme/theme-create.php:

  • Passed preservePatternSlugs => true in create_child_theme().

In tests/test-theme-templates.php:

  • Added unit tests covering reference conversion, child theme slug preservation, nested pattern expansion, filter overrides, and edited block retention.

Testing Instructions

Unit tests:

  1. Run npm run test:unit:php:setup to start wp-env.
  2. Run npm run test:unit:php:base -- --filter Test_Create_Block_Theme_Templates.
  3. Verify all tests pass with 0 failures.

Manual test:

  1. Activate Twenty Twenty-Five and enable Create Block Theme.
  2. In Site Editor, edit the Page template and insert the Centered heading pattern (twentytwentyfive/cta-centered-heading).
  3. Save changes in the editor, open the CBT sidebar, and click Save Changes to Theme.
  4. Check templates/page.html and verify the pattern is written as <!-- wp:pattern {"slug":"twentytwentyfive/cta-centered-heading"} /-->.
  5. Edit the heading text, save again via CBT, and verify the edited blocks remain inline.
  6. Create a child theme and verify pattern references retain the parent theme's slug.

Use of AI Tools

  • AI assistance: Used for identifying child theme edge cases, diagnosing Gutenberg attribute migrations, implementing normalization logic, and writing unit tests.
  • Tool(s) used: Claude Sonnet 4.6

@ishitaj34
ishitaj34 marked this pull request as ready for review November 12, 2026 07:04
@ishitaj34

Copy link
Copy Markdown
Author

The CI E2E Tests failure is an upstream runner issue (ubuntu-latest / Ubuntu 26.04 vs Playwright 1.58.2), unrelated to the PR.

All tests pass locally:

  • Playwright E2E: 6/6 passed

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.

Save unedited theme patterns in templates as pattern references

1 participant