You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
register_block_template(): PHP warnings when a plugin-registered template is assigned to an individual post #80291
When a template registered with the Plugin Template Registration API is assigned to an individual post, several PHP warnings and deprecation notices are triggered in the editor.
Step-by-step reproduction instructions
Register a block template from a plugin.
add_action(
'init',
function () {
register_block_template(
'gutenberg//author-admin',
array(
'title' => 'Enhanced Author Admin template',
'description' => 'An enhanced version of the Author Admin template.',
'content' => '<!-- wp:template-part {"slug":"header","tagName":"header"} /--><!-- wp:group {"tagName":"main","layout":{"inherit":true}} --><main class="wp-block-group"><!-- wp:paragraph --><p>This is an enhanced author template.</p><!-- /wp:paragraph --></main><!-- /wp:group --><!-- wp:template-part {"slug":"footer","tagName":"footer"} /-->',
'post_types' => array( 'post' ),
)
);
}
);
Open a post in the editor.
Assign the registered plugin template to that post.
Reload or reopen the editor.
Expected behavior
The assigned template should load without any PHP warnings or deprecation notices.
Actual behavior
The following warnings are generated:
Warning: Undefined array key 0 in wp-includes/block-editor.php on line 469
Warning: Attempt to read property "content" on null in wp-includes/block-editor.php on line 469
Deprecated: preg_match(): Passing null to parameter #2 ($subject) of type string is deprecated in wp-includes/class-wp-block-parser.php on line 247
Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in wp-includes/class-wp-block-parser.php on line 324
Additional information
The template is registered using the plugin-slug//template-slug naming convention introduced in #61577.
The same warnings also occur in WordPress 7.0, so this issue is not specific to WordPress 7.1.
If I open the assigned template in the Site Editor and save it (even without making any changes), the warnings no longer occur.
Hmmm the issue seems to stem from the get_block_templates() function.
When multiple templates are returned, the items are numerically indexed, which is OK as the template content is fetched from the first item (index [0])
However when only one matching template is returned, the single item is indexed with the template id.
Edit: actually it is a mix of numeric and string keys, with templates registered via register_block_template() (string keys) appended to the built-in templates (numeric keys).
I think a simple fix would be to re-index the array of templates returned from get_block_templates() at the point where core templates and plugin templates are combined/merged.
When the only matching template comes from WP_Block_Templates_Registry::get_by_query(), the returned array is keyed by the full registration name (for example, gutenberg//author-admin) rather than a numeric index. As a result, ! empty( $current_template ) evaluates to true, but $current_template[0] does not exist, triggering the Undefined array key 0 warning. That then causes null to be passed into parse_blocks(), leading to the subsequent preg_match() and strlen() deprecation notices.
This normalizes the merged array back to a zero-based numeric index, matching the expectations of existing consumers.
One additional note on where this fix belongs: get_block_templates(), WP_Block_Templates_Registry, and wp_get_post_content_block_attributes() all live in WordPress core—they are not duplicated in Gutenberg's lib/ directory. That means a Gutenberg PR alone won't ship the fix. The change needs to land in WordPress core (via a Trac ticket against wp-includes/block-template-utils.php), and this GitHub issue can then reference the corresponding core ticket.
One additional note on where this fix belongs: get_block_templates(), WP_Block_Templates_Registry, and wp_get_post_content_block_attributes() all live in WordPress core—they are not duplicated in Gutenberg's lib/ directory. That means a Gutenberg PR alone won't ship the fix. The change needs to land in WordPress core (via a Trac ticket against wp-includes/block-template-utils.php), and this GitHub issue can then reference the corresponding core ticket.
Should it also be fixed in the corresponding gutenberg_get_block_templates() "Template Activation" experiment
I have raised an upstream PR to address this issue, as the code has already been migrated from the Gutenberg repository to the WordPress Core repository. Here is the PR: WordPress/wordpress-develop#12557 (I have also added a couple of tests)
I actually tried creating a ticket about a week ago, but I couldn't find the "Create ticket" button then. I checked again today and couldn't find it either, I'm not sure why this is happening.
I actually tried creating a ticket about a week ago, but I couldn't find the "Create ticket" button then. I checked again today and couldn't find it either, I'm not sure why this is happening.
Activity
Andrew-Starr commentedon Jul 15, 2026
Hmmm the issue seems to stem from the
get_block_templates()function.When multiple templates are returned, the items are numerically indexed, which is OK as the template content is fetched from the first item (index [0])
However when only one matching template is returned, the single item is indexed with the template id.
Single template:
Multiple templates:
Edit: actually it is a mix of numeric and string keys, with templates registered via
register_block_template()(string keys) appended to the built-in templates (numeric keys).I think a simple fix would be to re-index the array of templates returned from
get_block_templates()at the point where core templates and plugin templates are combined/merged.Replace:
$query_result = array_merge( $query_result, $matching_registered_templates );With:
$query_result = array_values( array_merge( $query_result, $matching_registered_templates ) );vedant-ere commentedon Jul 15, 2026
Confirmed — I traced this independently and arrived at the same root cause.
The missing link is that the consumer which trips over the mixed-key array is
wp_get_post_content_block_attributes()inwp-includes/block-editor.php:When the only matching template comes from
WP_Block_Templates_Registry::get_by_query(), the returned array is keyed by the full registration name (for example,gutenberg//author-admin) rather than a numeric index. As a result,! empty( $current_template )evaluates totrue, but$current_template[0]does not exist, triggering theUndefined array key 0warning. That then causesnullto be passed intoparse_blocks(), leading to the subsequentpreg_match()andstrlen()deprecation notices.I agree with @Andrew-Starr's proposed fix:
This normalizes the merged array back to a zero-based numeric index, matching the expectations of existing consumers.
One additional note on where this fix belongs:
get_block_templates(),WP_Block_Templates_Registry, andwp_get_post_content_block_attributes()all live in WordPress core—they are not duplicated in Gutenberg'slib/directory. That means a Gutenberg PR alone won't ship the fix. The change needs to land in WordPress core (via a Trac ticket againstwp-includes/block-template-utils.php), and this GitHub issue can then reference the corresponding core ticket.Correct me if I am wrong somewhere
Andrew-Starr commentedon Jul 15, 2026
Should it also be fixed in the corresponding
gutenberg_get_block_templates()"Template Activation" experimentgutenberg/lib/compat/wordpress-7.0/template-activate.php
Line 821 in 60b366f
im3dabasia commentedon Jul 16, 2026
I have raised an upstream PR to address this issue, as the code has already been migrated from the Gutenberg repository to the WordPress Core repository. Here is the PR: WordPress/wordpress-develop#12557 (I have also added a couple of tests)
youknowriad commentedon Jul 22, 2026
Would you be able to check if there's a trac ticket for this and create one if not. That way it can be addressed and milestoned properly.
im3dabasia commentedon Jul 22, 2026
@youknowriad,
I actually tried creating a ticket about a week ago, but I couldn't find the "Create ticket" button then. I checked again today and couldn't find it either, I'm not sure why this is happening.
Here's the Slack reference
I also asked a couple of colleagues to check, and they ran into the same issue.
If possible, could someone else please open a ticket for this?
youknowriad commentedon Jul 22, 2026
Weird, let me do this
vedant-ere commentedon Jul 22, 2026
Same happend when I tried creating a ticket
And tried on different browsers and devices....
youknowriad commentedon Jul 22, 2026
I created this issue https://core.trac.wordpress.org/ticket/65689 (feel free to attach your PR to it)
For the ticket creation issue, apparently it's fixed per this thread but feel free to comment there if it's still happening.