register_block_template(): PHP warnings when a plugin-registered template is assigned to an individual post
Description
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 https://github.com/WordPress/gutenberg/pull/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.
Screenshots, screen recording, code snippet
No response
Environment info
WordPress 7.1-alpha-62745 (current trunk) PHP: 8.4.11 Theme Twenty Twenty-Five
Please confirm that you have searched existing issues in the repo.
- [x] Yes
Please confirm that you have tested with all plugins deactivated except Gutenberg.
- [x] Yes
Please confirm which theme type you used for testing.
- [x] Block
- [ ] Classic
- [ ] Hybrid (e.g. classic with theme.json)
- [ ] Not sure
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:
Array
(
[gutenberg//author-admin] => WP_Block_Template Object
Multiple templates:
Array
(
[0] => WP_Block_Template Object
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 ) );
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() in wp-includes/block-editor.php:
$current_template = get_block_templates( array( 'slug__in' => array( $template_slug ) ) );
if ( ! empty( $current_template ) ) {
$template_blocks = parse_blocks( $current_template[0]->content ); // assumes numeric key 0
}
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.
I agree with @Andrew-Starr's proposed fix:
$query_result = array_values( array_merge( $query_result, $matching_registered_templates ) );
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.
Correct me if I am wrong somewhere
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.
Should it also be fixed in the corresponding gutenberg_get_block_templates() "Template Activation" experiment https://github.com/WordPress/gutenberg/blob/60b366f02f3fc5095226d3888b425b8c231b5fe2/lib/compat/wordpress-7.0/template-activate.php#L821
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: https://github.com/WordPress/wordpress-develop/pull/12557 (I have also added a couple of tests)
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.
@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?
Weird, let me do this
@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?
Same happend when I tried creating a ticket And tried on different browsers and devices....
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.