Skip to content

Commit 7158d9f

Browse files
committed
Block Hooks: Apply block hooks to plugin-registered templates.
As of WordPress 6.7.0, it is possible -- e.g. for plugins -- to register block templates via a new API, `register_block_template()`. Unlike block templates loaded from theme files or from the database, however, those block templates didn't have Block Hooks applied. This changeset rectifies this inconsistency. Props iamadisingh, aljullu, bernhard-reiter. Fixes #63808. git-svn-id: https://develop.svn.wordpress.org/trunk@60688 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 56d7d59 commit 7158d9f

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

src/wp-includes/block-template-utils.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,20 @@ function ( $registered_template ) use ( $template_files ) {
12271227
return true;
12281228
}
12291229
);
1230-
$query_result = array_merge( $query_result, $matching_registered_templates );
1230+
1231+
$matching_registered_templates = array_map(
1232+
function ( $template ) {
1233+
$template->content = apply_block_hooks_to_content(
1234+
$template->content,
1235+
$template,
1236+
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
1237+
);
1238+
return $template;
1239+
},
1240+
$matching_registered_templates
1241+
);
1242+
1243+
$query_result = array_merge( $query_result, $matching_registered_templates );
12311244
}
12321245
}
12331246

@@ -1323,9 +1336,10 @@ function get_block_template( $id, $template_type = 'wp_template' ) {
13231336
}
13241337

13251338
/**
1326-
* Retrieves a unified template object based on a theme file.
1339+
* Retrieves a unified template object based on a theme file or plugin registration.
13271340
*
13281341
* This is a fallback of get_block_template(), used when no templates are found in the database.
1342+
* Also checks for templates registered via the Template Registration API.
13291343
*
13301344
* @since 5.9.0
13311345
*
@@ -1371,6 +1385,14 @@ function get_block_file_template( $id, $template_type = 'wp_template' ) {
13711385

13721386
$block_template = WP_Block_Templates_Registry::get_instance()->get_by_slug( $slug );
13731387

1388+
if ( $block_template ) {
1389+
$block_template->content = apply_block_hooks_to_content(
1390+
$block_template->content,
1391+
$block_template,
1392+
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
1393+
);
1394+
}
1395+
13741396
/**
13751397
* Filters the block template object after it has been (potentially) fetched from the theme file.
13761398
*

0 commit comments

Comments
 (0)