Skip to content

fix: Use template title and category slugs in pattern headers - #880

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

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

Conversation

@ishitaj34

Copy link
Copy Markdown

What?

Closes #871

Fixes two issues in the pattern file header generation:

  1. pattern_from_template was using the template slug as the title instead of the human-readable $template->title label shown in the Site Editor.
  2. pattern_from_wp_block was writing pattern category names into the Categories header instead of taxonomy slugs.

Why?

For example, when a user exported a theme, the generated pattern .php files had:

  • Title: archive-product instead of Title: Product Catalog, making patterns hard to identify in the inserter.
  • Categories: Banners instead of Categories: banner, breaking category filtering in the block inserter since Core matches by slug.

How?

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

  • pattern_from_template: reads $template->title when present, falls back to $template->slug if absent or empty. Applies the existing */ → */ escaping to the title as well.
  • pattern_from_wp_block: changes wp_list_pluck( $pattern_category_list, 'name' ) to wp_list_pluck( $pattern_category_list, 'slug' ).

In tests/test-theme-patterns.php:

  • test_pattern_from_template_uses_title_not_slug: Checks that the pattern title uses the human-readable title of the template over its slug.
  • test_pattern_from_template_falls_back_to_slug_when_title_empty: Checks that an empty or missing title falls back to the slug.
  • test_pattern_from_template_escapes_comment_closer_in_title: Checks that */ in the title is safely escaped to */, preventing the docblock of the pattern file from closing prematurely and causing PHP syntax errors or code injection.
  • test_pattern_from_wp_block_uses_category_slugs_not_names: Checks that categories writes the category slug instead of its display name.
  • test_pattern_from_wp_block_uses_multiple_category_slugs: Checks that multiple categories are exported as comma-separated slugs.

Testing Instructions

Unit tests:

  1. Run npm run test:unit:php:setup (starts wp-env).
  2. Run npm run test:unit:php:base -- --filter Test_Create_Block_Theme_Patterns.
  3. All 27 tests should pass with 0 failures.
  4. To specifically run the 5 new tests added above:
npm run test:unit:php:base -- --filter 'test_pattern_from_template_uses_title_not_slug|test_pattern_from_template_falls_back_to_slug_when_title_empty|test_pattern_from_template_escapes_comment_closer_in_title|test_pattern_from_wp_block_uses_category_slugs_not_names|test_pattern_from_wp_block_uses_multiple_category_slugs'

Manual test:

  1. Activate a block theme (e.g. Twenty Twenty-Five).
  2. For template title: Appearance -> Editor -> Templates. Open any template, and verify it has a human-readable title.
  3. For category slugs: Appearance → Editor → Patterns. Create a new pattern assigned to a category.
  4. Go to Appearance -> Create Block Theme and export the theme as a zip.
  5. Unzip and open the corresponding patterns/*.php. The Title: header should match the Site Editor label, not the file slug, and Categories: should match the file slug.

Use of AI Tools

  • AI assistance: Used for drafting tests along with edge cases, and PR description.
  • Tool(s) used: Claude Sonnet 4.6

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.

Generated pattern headers use the template slug as title and category names instead of slugs

1 participant