Make WordPress Core

Opened 4 months ago

Last modified 6 days ago

#64123 new enhancement

Patterns: add the pattern name to pattern blocks when they are converted

Reported by: ramonopoly's profile ramonopoly Owned by:
Milestone: 7.0 Priority: normal
Severity: normal Version:
Component: Editor Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

The editor has used metadata in pattern attributes since https://github.com/WordPress/gutenberg/pull/59251 to organize and sort patterns.

Patterns metadata allows the editor to distinguish patterns from other block groups, and from other patterns.

An initiative to introduce a special "simple" content only mode to patterns depends on this feature. For user-created and inserted patterns, the editor will render the block tree differently, and present specific controls and tools to the user.

See: See: https://github.com/WordPress/gutenberg/issues/71517

The feature is slated for WordPress 7.0.

For this feature to work however, the block editor needs to know that it is dealing with a pattern.

For patterns created in the editor, and for patterns returned by the patterns REST API endpoint, the status of "pattern" can be inferred.

However, many templates contain patterns, for example TT5's 404 template

https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-content/themes/twentytwentyfive/templates/404.html#L5

The block editor could parse templates on the frontend to identify patterns upon insertion, but this would have an impact on performance, especially for large patterns.

Rather, let's leverage the existing patterns parser resolve_pattern_blocks to add this metadata to theme patterns before they're sent as a response from the REST APIs.

See: https://github.com/WordPress/wordpress-develop/blob/trunk/src/wp-includes/blocks.php#L1843

Change History (8)

This ticket was mentioned in PR #10248 on WordPress/wordpress-develop by @ramonopoly.


4 months ago
#1

Builds upon https://github.com/WordPress/wordpress-develop/pull/10180

Props to @scruffian

Modifies the resolve_pattern_blocks function to include metadata for single-root patterns, allowing the pattern name and title to be stored in the block attributes. The metadata contains the patternName and name attributes.

This enables identification of patterns inside templates and allows for special treatment in the editor.

This will be used to test contentOnly editing (https://github.com/WordPress/gutenberg/issues/71517), so that themes that use patterns automatically opt into this behaviour.

Trac ticket: https://core.trac.wordpress.org/ticket/64123

@scruffian commented on PR #10248:


4 months ago
#2

Is there other metadata we could/should add to patterns - some of them seem to also contain a longer description. It would be good to support as much metadata as possible IMO.

@ramonopoly commented on PR #10248:


4 months ago
#3

Is there other metadata we could/should add to patterns - some of them seem to also contain a longer description. It would be good to support as much metadata as possible IMO.

Good question. What do you think the editor might need? There are also optional categories and block types, which could come in handy if the editor ever wanted to extend "change design" to patterns bundled with templates.

I'm not sure.

@scruffian commented on PR #10248:


4 months ago
#4

The description would be useful for https://github.com/WordPress/gutenberg/pull/72574. Not sure about the others.

@ramonopoly commented on PR #10248:


4 months ago
#5

The description would be useful for https://github.com/WordPress/gutenberg/pull/72574.

Done!

This ticket was mentioned in Slack in #core by audrasjb. View the logs.


6 days ago

#7 @audrasjb
6 days ago

  • Keywords needs-testing added

As per today's 7.0 bug scrub: as we have a PR ready to test, I'm adding the related keyword.

#8 @huzaifaalmesbah
6 days ago

  • Keywords needs-testing removed

Patch Testing Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/10248

Environment

  • WordPress: 7.0-alpha-61215-src
  • PHP: 8.2.30
  • Server: nginx/1.29.4
  • Database: mysqli (Server: 9.5.0 / Client: mysqlnd 8.2.30)
  • Browser: Chrome 144.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.4
  • MU Plugins: None activated
  • Plugins:
    • Classic Editor 1.6.7
    • Hello Dolly 1.7.2
    • Test Reports 1.2.1

Steps taken

  1. Started with a clean WordPress trunk install.
  2. Activated Twenty Twenty-Five (block theme).
  3. Navigated to: /wp-admin/site-editor.php
  4. Opened: Templates → Twenty Twenty-Five → Page: 404
  5. Clicked the 3-dot menu → Edit → Code editor.
  6. Observed the resolved pattern block markup.
  1. Before applying the patch: The resulting root block contains NO metadata.
  2. Applied PR.
  3. Repeated the same steps and checked Code editor again.
  1. ✅ After applying the patch:
  • The resolved root block now includes metadata.
  • Found: "metadata":{"patternName":"twentytwentyfive/hidden-404","name":"404"}

Expected result

  • Pattern blocks converted inside templates should retain pattern metadata
  • Root block should contain: metadata.patternName and metadata.name
  • Editor can distinguish patterns from normal block groups

Additional Notes

  • Verified behavior only affects resolved blocks in the Site Editor (not theme template files directly).
  • Tested saving, refreshing, and reopening — metadata remains intact.
  • No performance or UI regressions observed.

Screenshots/Screencast with results

Before and after behavior video: https://files.catbox.moe/d1uxje.mp4

Before Apply Patch After Apply Patch
https://i.ibb.co/Jwyb5mSF/Huzaifa-20260204112435.png https://i.ibb.co/9kDKyN8W/Huzaifa-20260204112545.png
Note: See TracTickets for help on using tickets.