WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.54k stars 4.21k forks source link

Allow the new `wp_register_block_template` to register both `wp_template` and `wp_template_part` types #65861

Open daviedR opened 1 month ago

daviedR commented 1 month ago

What problem does this address?

Hi, I just checked the new wp_register_block_template API (WP 6.7 beta) that allows custom plugins (and themes) to register block templates in PHP.

I noticed that the API only supports creating block templates (wp_template) as the type property is hardcoded in the registry (WP_Block_Templates_Registry).

In some cases, plugins might want to add block template parts (wp_template_part) in PHP. For example, the WooCommerce plugin adds a Mini Cart block template part. And this can't be done using the new wp_register_block_template API.

What is your proposed solution?

Allow the wp_register_block_template API to also register a block template part. Perhaps by defining the type property in the $args parameter.

Currently, the get_block_templates function in the core can return either wp_template or wp_template_part objects based on the specified template_type parameter. And the returned objects for both types have similar structures. So maybe we can use the wp_register_block_template API to also register wp_template or wp_template_part type, instead of creating a separate API for template parts (e.g. wp_register_block_template_part).


Also, I am curious why is it named wp_register_block_template instead of register_block_template? We have similarly named APIs, like register_block_type, register_block_style, register_block_pattern, etc.

rudrakshi-gupta commented 1 month ago

Hi, I would like to work on this issue.

youknowriad commented 1 month ago

Ideally, we want to try to make template parts, special block patterns. Read this for more details https://github.com/WordPress/gutenberg/issues/62566#issuecomment-2180749161

In that sense, there's already a registration function for patterns. So solving what that link proposes would be another way to solve this issue.