Skip to content

Editor: Fix fatal when a plugin-registered block template is selected - #15864

Open
sarequl wants to merge 3 commits into
WordPress:trunkfrom
sarequl:fix/64216-registered-template-array-keys
Open

sarequl wants to merge 3 commits into
WordPress:trunkfrom
sarequl:fix/64216-registered-template-array-keys

Conversation

@sarequl

@sarequl sarequl commented Nov 10, 2026

Copy link
Copy Markdown

Fixes a fatal "Undefined array key 0" in the editor when a plugin-registered block template is included in the current template list.

See https://core.trac.wordpress.org/ticket/64216

What

get_block_templates() merges plugin-registered templates into the query results with array_merge(). WP_Block_Templates_Registry::get_by_query() keys its result by template name, so array_filter()/array_map() preserve those string keys and the merged array is no longer a list. wp-includes/block-editor.php then reads $current_template[0]->content, which is undefined, so the editor dies with "Undefined array key 0" when a plugin-registered template is among the matches.

Why

Plugin-registered templates are a supported extension point, and selecting one as the current template must not fatal. The reporter's diagnosis and suggested fix — wrap the registered templates in array_values() — is confirmed in code: the registry keys results by template name, so reindexing before the merge restores the list shape the editor expects.

How

In src/wp-includes/block-template-utils.php, array_merge( $query_result, $matching_registered_templates ) becomes array_merge( $query_result, array_values( $matching_registered_templates ) ). This reindexes the registered templates so the merged result is a list, matching the rest of the template API. No other behaviour changes.

Adds test_get_block_templates_should_return_a_list_when_a_plugin_registered_template_is_included in tests/phpunit/tests/blocks/getBlockTemplates.php with @ticket 64216, asserting array_is_list(), assertCount( 1, ... ) and $templates[0]->slug.

Testing Instructions

  1. Run the tests: vendor/bin/phpunit tests/phpunit/tests/blocks/getBlockTemplates.php → OK (8 tests, 26 assertions).
  2. Negative control: revert the one source line (git checkout <base> -- src/wp-includes/block-template-utils.php) and re-run → Tests: 8, Assertions: 24, Failures: 1, failing exactly test_get_block_templates_should_return_a_list_when_a_plugin_registered_template_is_included. Restoring the fix makes the run green again.
  3. Broader regression: vendor/bin/phpunit --test-suffix .php tests/phpunit/tests/blocks/ → OK (576 tests, 3151 assertions).
  4. phpcs on the two changed files: vendor/bin/phpcs --standard=phpcs.xml.dist src/wp-includes/block-template-utils.php tests/phpunit/tests/blocks/getBlockTemplates.php → exit 0.
  5. composer lint and composer phpstan show only pre-existing violations/errors (identical on base and branch); npm run build passes.

WP_Block_Templates_Registry::get_by_query() returns templates keyed by
template name. array_merge() preserves string keys, so a query that
matched only a plugin-registered template returned a non-list array and
block-editor.php could not read $current_template[0].

Wrap the registered templates in array_values() so the merged result
stays a list. See #64216.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs.

Core Committers: Use this line as a base for the props when committing in SVN:

Props sarequl.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

@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.

sarequl and others added 2 commits November 10, 2026 04:26
get_block_templates() now returns a sequentially indexed list, so a registered template is no longer keyed by its full name. Assert the template is present via its plugin instead of relying on the old string key, which also prevented the registration from being cleaned up when the assertion failed.
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