Skip to content

fix(export): register template parts with area and title in theme.json on export and save. - #883

Open
KarunyaChavan wants to merge 2 commits into
WordPress:trunkfrom
KarunyaChavan:fix/export-template-parts-theme-json
Open

KarunyaChavan wants to merge 2 commits into
WordPress:trunkfrom
KarunyaChavan:fix/export-template-parts-theme-json

Conversation

@KarunyaChavan

@KarunyaChavan KarunyaChavan commented Oct 9, 2026 •

Copy link
Copy Markdown

This PR fixes an issue where saving or exporting template parts wrote the template markup files to parts/*.html but failed to register corresponding templateParts definitions in theme.json. As a result, custom template parts created in the Site Editor lost their assigned areas (such as Header, Footer, or Sidebar) and human-friendly titles upon export or when database customizations were cleared, falling back to "Uncategorized" and raw slug titles.

Fixes #870

Why ?

  • Loss of Area Classification: When users create template parts in the Site Editor or customize plugin-provided parts, WordPress stores the area (header, footer, sidebar) and custom title in the database. Without registering them in theme.json, WordPress Core defaults to WP_TEMPLATE_PART_AREA_UNCATEGORIZED.
  • Broken Editor Organization: Upon exporting the theme as a ZIP or sharing it, users cannot find parts under their expected category in the Editor inserter (e.g., header replacements do not show up under Header variations).
  • Missing Sync on Local Save: When users choose "Save Changes" to save templates to the active theme files, only the HTML files in parts/ were written; theme.json was never updated to reflect the newly created parts.

What Changed ?

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

    • Added add_template_parts_to_theme_json_data(): Merges template parts into the theme.json data array under templateParts. It populates name, title, and area, safely falls back to slug name and 'uncategorized' if properties are empty, and deduplicates by slug so existing definitions are preserved.
    • Added update_theme_json_template_parts(): Updates theme.json on disk in the target theme directory, writes the formatted JSON, and clears CBT_Theme_JSON_Resolver and wp_get_theme() caches.
    • Updated add_templates_to_local(): Calls update_theme_json_template_parts() whenever template parts are saved locally, ensuring theme.json is updated even if the user saves templates without saving global styles.
  • includes/create-theme/resolver_additions.php:

    • Updated CBT_Theme_JSON_Resolver::export_theme_data(): Merges exported template parts into theme.json data during ZIP export, clone, and child theme creation based on the active export scope (all, current, or user).
  • tests/test-theme-templates.php:

    • Added 5 unit tests covering:
      • Adding new template parts with name, title, and area.
      • Deduplicating by slug without overwriting existing entries.
      • Defaulting missing titles and areas safely.
      • Writing and persisting changes directly to theme.json on disk.
      • Export integration via CBT_Theme_JSON_Resolver::export_theme_data().

Steps for Testing

  • Test 1: Local Save Changes

    • Activate a block theme (e.g., Twenty Twenty-Four).
    • In Site Editor > Patterns > Template Parts, create a new template part (e.g., Title: Header Variation V2, Area: Header, Slug: header-variation-v2).
    • Open the Create Block Theme sidebar and select Save Changes (ensure templates/parts are checked).
    • Inspect the active theme's theme.json file on disk.
    • Verify: A templateParts array exists containing an entry for header-variation-v2 with "area": "header" and "title": "Header Variation V2".
  • Test 2: ZIP Export

    • In the Site Editor, open Create Block Theme and choose Export {Theme Name}.
    • Unzip the downloaded file and open theme.json.
    • Verify: The exported theme.json includes the new part in templateParts with its title and area intact.

Use of AI

  • AI Used: Yes
  • Tool: Antigravity
  • Model: Gemini 3.1 Pro
  • Used For: Adding unit test for updated patch and adding details to PR description.

When exporting or saving templates to theme files, parts/*.html files were written to disk, but corresponding entries in theme.json under templateParts were never registered. This caused custom template parts or plugin-provided parts (e.g., WooCommerce mini-cart) to lose their area classification and human-friendly titles, falling back to 'uncategorized' and raw slug names.

- Add CBT_Theme_Templates::add_template_parts_to_theme_json_data() to merge exported template parts with their name, title, and area, deduplicating by slug and preserving existing entries.
- Add CBT_Theme_Templates::update_theme_json_template_parts() to write templateParts directly to local theme.json during add_templates_to_local() and invalidate theme JSON resolver caches.
- Update CBT_Theme_JSON_Resolver::export_theme_data() to merge exported template parts into theme.json during ZIP exports and theme cloning.
…eme.json.

- test_add_template_parts_to_theme_json_data_adds_new_parts: Verifies new template parts are added with name, title, and area.
- test_add_template_parts_to_theme_json_data_does_not_duplicate_existing: Ensures existing templateParts entries in theme.json are preserved without duplicates.
- test_add_template_parts_to_theme_json_data_defaults_area_and_title: Verifies safe fallbacks to slug and 'uncategorized' when title or area are missing.
- test_update_theme_json_template_parts_updates_file: Verifies file-level theme.json update and persistence on disk.
- test_export_theme_data_includes_template_parts: Verifies integration with CBT_Theme_JSON_Resolver::export_theme_data().
@KarunyaChavan
KarunyaChavan marked this pull request as ready for review October 9, 2026 09:46
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.

Exported template parts aren't registered in theme.json templateParts

1 participant