Skip to content

get_block_templates(): Fix non-sequential array keys for registry-only template matches#12557

Draft
im3dabasia wants to merge 2 commits into
WordPress:trunkfrom
im3dabasia:fix/80291-upstream-patch
Draft

get_block_templates(): Fix non-sequential array keys for registry-only template matches#12557
im3dabasia wants to merge 2 commits into
WordPress:trunkfrom
im3dabasia:fix/80291-upstream-patch

Conversation

@im3dabasia

@im3dabasia im3dabasia commented Jul 16, 2026

Copy link
Copy Markdown

Trac ticket:

What Why How

get_block_templates() returns an array whose keys are not sequential when the only matching template comes from the plugin template registry (WP_Block_Templates_Registry), which stores templates keyed by name. When no wp_template post or theme file also matches, array_merge() carries those string keys straight into the result with no numeric 0 index.

This breaks callers that assume a plain list, e.g. wp_get_post_content_block_attributes() in block-editor.php, which accesses $templates[0]. Reproduced in WordPress/gutenberg#80291 : assigning a plugin-registered template to a post and opening it in the editor triggers PHP warnings and deprecation notices.

Two tests added:

  • test_get_block_templates_returns_sequentially_indexed_array_for_registry_only_match - asserts get_block_templates() returns a sequentially indexed array when the registry is the sole match.
  • test_wp_get_post_content_block_attributes_with_registry_only_assigned_template - reproduces the original issue end-to-end.

Before

both the tests I added were failing locally.

After

image image

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Model(s): Claude Sonnet 4.5

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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.

1 participant