Repository navigation
Fix Save Patterns deleting unrelated template and template-part customizations - #913
Open
KhushalSainS wants to merge 2 commits into
Open
KhushalSainS wants to merge 2 commits into
KhushalSainS wants to merge 2 commits into
Conversation
KhushalSainS
marked this pull request as ready for review
November 16, 2026 15:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this fixes
Save Patterns removed every custom template and template part from the database, not just the ones written to the theme. Templates that did not reference a saved pattern were deleted with no warning.
This change clears only the templates and template parts that were actually written to disk during the save, and only once, after all patterns have been processed.
Fixes #912
Root cause
CBT_Theme_Patterns::replace_local_pattern_references()calledclear_user_templates_customizations()andclear_user_template_parts_customizations()without arguments from inside the per-pattern loop. Without arguments those helpers clear every custom template and template part, so each pattern iteration re-cleared the whole set. The collected slugs also mixedwp_templateandwp_template_part, so a template namedpagecould delete an unrelated part namedpage.Changes
includes/create-theme/theme-patterns.phpwp_template,wp_template_part) instead of one mixed list.clear_saved_pattern_template_customizations()..htmlfile exists on disk, so a failed write never leads to a cleared customization.glob()results before looping and usebasename()in the duplicate-pattern check so it cannot match a partial path.includes/create-theme/theme-templates.phpget_theme_templates()andadd_templates_to_local()accept an optional$post_type.add_templates_to_local()returns early for an empty slug list, instead of exporting every template (WordPress treats an emptyslug__inas no filter).clear_user_templates_customizations()andclear_user_template_parts_customizations()accept an optional$slugslist and skip entries without awp_id.tests/test-theme-patterns.phpManual before/after
Same WordPress site, same seeded data, Save Patterns on / Save Template Changes off:
<!-- wp:pattern {"slug":"theme/pattern"} /-->Also verified with one template referencing two patterns: the saved file contains two local pattern references, zero
wp:block {"ref":...}references, and both pattern files are created.