Skip to content

Template Activation: fix array keys in get_block_templates() when merging registered templates - #80351

Closed
vedant-ere wants to merge 1 commit into
WordPress:trunkfrom
vedant-ere:fix/registered-block-templates-array-keys
Closed

vedant-ere wants to merge 1 commit into
WordPress:trunkfrom
vedant-ere:fix/registered-block-templates-array-keys

Conversation

@vedant-ere

Copy link
Copy Markdown
Contributor

What?

Fixes the PHP warnings described in #80291, for the Template Activation experiment's copy of get_block_templates().

Why?

gutenberg_get_block_templates() (in lib/compat/wordpress-7.0/template-activate.php, hooked onto pre_get_block_templates) merges plugin-registered templates into the query result with array_merge():

$query_result = array_merge( $query_result, $matching_registered_templates );

WP_Block_Templates_Registry::get_by_query() returns its matches keyed by the full registered template name (e.g. my-plugin//my-template), not a numeric index. array_merge() preserves non-numeric string keys instead of renumbering them, so when a plugin-registered template is the only match for a slug__in query, $query_result ends up looking like:

[ 'my-plugin//my-template' => WP_Block_Template Object ]

instead of the expected:

[ 0 => WP_Block_Template Object ]

Every caller of get_block_templates() reads the first match as $templates[0] (for example core's wp_get_post_content_block_attributes()). With a string-keyed result, that produces Undefined array key 0 / Attempt to read property "content" on null, and the resulting null flowing into parse_blocks() then trips the preg_match() / strlen() deprecation notices reported in the issue.

How?

Wrap the merge in array_values() so the result is always re-indexed from 0, regardless of whether the match came from the DB query, a theme file, or the plugin registry.

Testing Instructions

  1. Enable the active_templates experiment.
  2. Register a template from a plugin using register_block_template( 'my-plugin//my-template', [...] ).
  3. Call get_block_templates( [ 'slug__in' => [ 'my-template' ] ] ) and confirm the single result is $templates[0], not keyed by the registration name.
  4. Run phpunit/class-gutenberg-get-block-templates-registered-template-test.php — it fails before this patch and passes after.

Note on scope

This only fixes the Template Activation experiment's copy of the function. The bug reported in #80291 by default (without the experiment enabled) comes from core's own get_block_templates() in wp-includes/block-template-utils.php, which isn't duplicated in this plugin's lib/ directory — that half needs a separate WordPress core Trac ticket and patch, tracked from the GitHub issue.

…ing registered templates

gutenberg_get_block_templates() merges registered plugin templates into the
query result with array_merge(). WP_Block_Templates_Registry::get_by_query()
returns its matches keyed by the full registered template name (e.g.
my-plugin//my-template), and array_merge() preserves those non-numeric string
keys instead of renumbering them. When a plugin-registered template is the
only match for a slug__in query, the result ends up keyed by that string
instead of 0.

Callers such as core's wp_get_post_content_block_attributes() read
$templates[0] directly, so this produces "Undefined array key 0" and
"Attempt to read property on null" warnings, which cascade into
parse_blocks() deprecation notices when null content reaches the parser.

Wrap the merge in array_values() so the result is always a zero-based list,
matching what every consumer of get_block_templates() expects.

See WordPress#80291
@vedant-ere
vedant-ere requested a review from spacedmonkey as a code owner July 16, 2026 10:05
@github-actions

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@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. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: vedant-ere <vedantere@git.wordpress.org>

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

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Jul 16, 2026
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @vedant-ere! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@youknowriad

Copy link
Copy Markdown
Contributor

That experiment has been removed for now #82241 so I'm closing the issue for now. Thanks for the contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants