Allow plugins to add new items to the "Add new" menu on the Site Editor
What?
This PR adds the new extraTemplateTypes custom setting and the extra_template_types filter which will allow plugins to add new items to the Add new dropdown menu on the Site Editor.
Why?
Currently, other plugins cannot add new menu items to the Add new menu.
For example, via the WooCommerce plugin, we want to make a block template available in the Add menu user interface that will apply to all product attributes pages, which wouldn't be possible right now.
How?
This PRs is introducing a new filter extra_template_types which plugins can use to add their custom templates and that is passed to the Site Editor via a new custom setting called extraTemplateTypes.
Testing Instructions
- Add a new template using the
extra_template_typeshook:
public function add_extra_template_types( $extra_template_types ) {
$extra_template_types[] = array(
'slug' => 'my_new_template',
'title' => 'My new template',
'description' => 'Displays my new template',
);
return $extra_template_types;
}
add_filter( 'extra_template_types', array( $this, 'add_extra_template_types' ) );
- Go to the
Site Editor>Browse all templates(/wp-admin/site-editor.php?postType=wp_template) - Click on the
Add newbutton and make sure you see an item in the dropdown for theMy new templatetemplate.
Screenshots
| Before | After |
|---|---|
![]() |
![]() |
Rather than plugins having to do this manually, could core handle it for them? IE if a custom post type or taxonomy is public then it automatically becomes possible to add templates for them.
The same functionality could theoretically drive https://github.com/WordPress/gutenberg/issues/40941.
Rather than plugins having to do this manually, could core handle it for them? IE if a custom post type or taxonomy is
publicthen it automatically becomes possible to add templates for them.
I'm also wondering this. It would be nice if we could use existing WordPress APIs.
Rather than plugins having to do this manually, could core handle it for them? IE if a custom post type or taxonomy is public then it automatically becomes possible to add templates for them.
I'm also wondering this. It would be nice if we could use existing WordPress APIs.
This is already handled for CPTs and custom taxonomies. It might be just specialized custom templates although I'm not sure what these should be and if it makes sense, as their content can't know the used headers/footers etc..
@albarin can you explain a bit more about the use case of yours and if the CPTs and taxonomies can be leveraged there?
--edit
There is also this issue about the addition of such templates: https://github.com/WordPress/gutenberg/issues/41401
@albarin can you explain a bit more about the use case of yours and if the CPTs and taxonomies can be leveraged there?
Sure, via the WooCommerce plugin, we added support for the Product by attribute template and we want it to be available from the Add menu (as it happens now with the Category and Tag taxonomies). This template applies to all product attribute pages.
I think internally product attributes are taxonomies, but each of them is a separate taxonomy (like pa_color or pa_size), and they are not public. But even if they were and appeared in the menu we want a template to apply to all of them, I don't see how we can do that.
Hope it makes some sense. Thanks all for your ideas. 🙏
@ntsekouras anything else we can do to get more feedback or move this forward? Thanks! 🙌
@ntsekouras anything else we can do to get more feedback or move this forward? Thanks! 🙌
I think you could bring it up in the weekly core-editor slack meeting. Unfortunately it seems that it might be very difficult to land for 6.2(with the feature freeze so close), but we do need to get more feedback in this one.
I think internally product attributes are taxonomies, but each of them is a separate taxonomy (like pa_color or pa_size), and they are not public. But even if they were and appeared in the menu we want a template to apply to all of them, I don't see how we can do that.
Have you explored the possibility of making them public? If that part is done, what would be the remaining problem? Would it be that you don't want to expose the single terms templates or the the general one?
Have you explored the possibility of making them public? If that part is done, what would be the remaining problem? Would it be that you don't want to expose the
singleterms templates or the general one?
@ntsekouras Yes, I've tried that and this is what happens: because every product attribute is a separate taxonomy itself we get an item in the menu for each one of them 👇 But what we want is a general template that will be applied to all product attributes.

Rather than plugins having to do this manually, could core handle it for them? IE if a custom post type or taxonomy is
publicthen it automatically becomes possible to add templates for them.
I think there are always cases that don't make sense to be supported by core. For example, the Product by Attribute has been discussed above. Besides, @albarin also linked an exploration PR about templates for each Product Type which is a private taxonomy under the hood. The Product Type template is for displaying individual products. We can make that taxonomy public but it's super confusing to add a new template for taxonomy but that template is used for the single item (instead of the loop).
We're just taking WooCommerce examples, I believe there is so much more special and creative usage of WordPress data objects in the broader ecosystem. And I think it'd be a great developer experience to support those custom/creative usages.
Thank you for clarifying some of the needed use cases. What becomes more obvious to me is that we should consider an API that could be more flexible in terms of the current generic template handling. This could mean an API that explicitly declares some things - for example if has a general template etc..
I think some of the use cases here are worth being mentioned to the scaling add template menu issue.
Sorry for delaying to response, but I'm mostly working for WP 6.2 right now..
For example, the Product by Attribute has been discussed above
I don't understand why this is an issue. You might want to create a template that is applied to all product attributes. It would be extremely tedious to have to repeat this process for every single attribute if you want the same display for all of them.
Product Type which is a private taxonomy under the hood
Private entities shouldn't be displayed here. Similar to how we do not offer template creation for private post types (like orders). Edit: That said, it seems reasonable that folks might want different templates for different product types.
Private entities shouldn't be displayed here. Similar to how we do not offer template creation for private post types (like orders). Edit: That said, it seems reasonable that folks might want different templates for different product types.
I just want to clarify my point more. Continuing on the Product Type, it's a special entity of WooCommerce that is only used internally. WC uses that taxonomy to determine the type of product. There isn't an archive page for product types.
Now we want to create templates for product types, we can't do that because Gutenberg only supports default entities that are used in the intended way (post type for a single item, taxonomy term for a list of items), we want to create the template for each product type which displays a single product but the entity powering it is a taxonomy, which for list of items.
Plugins should try to use the core entity as much as they can. But to me, using core entities in an unintended way like this makes total sense. Plugins can even create their own type of entity. So having a way to register custom templates will save plugin developers a lot of effort.

