Opened 5 weeks ago
#64216 new defect (bug)
Undefined array key 0 in wp-includes/block-editor.php on line 468
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Milestone: | Awaiting Review | Priority: | normal |
| Severity: | normal | Version: | 6.8.3 |
| Component: | Editor | Keywords: | |
| Focuses: | Cc: |
Description
Hi there,
I'm using register_block_template (with WordPress version 6.8.3) to register a custom template from within a plugin for a specific custom post type (the template slug I chose isn't in the single-{cpt_name} form, not sure it matters...).
The new template shows up in the template picker (select) on the cpt block editor screen, but as soon as I choose it, I get the following PHP warning logged:
[07-Nov-2025 13:55:18 UTC] PHP Warning: Undefined array key 0 in /shared/httpd/wptest/wordpress/wp-includes/block-editor.php on line 468
I've found the issue being here:
https://github.com/WordPress/WordPress/blob/bc7f6b462ddd8cfb4a095da8a9af9311e4bca3ab/wp-includes/block-template-utils.php#L1243
where $query_result, a standard array (of WP_Template), is merged with $matching_registered_templates, which is basically the associative array in the form
$template_name => $template pairs
resulting from https://github.com/WordPress/WordPress/blob/bc7f6b462ddd8cfb4a095da8a9af9311e4bca3ab/wp-includes/block-template-utils.php#L1218
see:
https://developer.wordpress.org/reference/classes/wp_block_templates_registry/get_by_query/#return
The issue (warning) could be easily fixed by turning
https://github.com/WordPress/WordPress/blob/bc7f6b462ddd8cfb4a095da8a9af9311e4bca3ab/wp-includes/block-template-utils.php#L1243
Into:
$query_result = array_merge( $query_result, array_values( $matching_registered_templates ) );
This only happens in the block editor but I'm not sure I should open the issue on https://github.com/WordPress/gutenberg/issues/new/choose as suggested, because the code generating the issue is in wp core, right?