Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow plugins to add new items to the "Add new" menu on the Site Editor #46362

Closed

Conversation

albarin
Copy link
Contributor

@albarin albarin commented Dec 7, 2022

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

  1. Add a new template using the extra_template_types hook:
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' ) );
  1. Go to the Site Editor > Browse all templates (/wp-admin/site-editor.php?postType=wp_template)
  2. Click on the Add new button and make sure you see an item in the dropdown for the My new template template.

Screenshots

Before After
before after

@albarin albarin force-pushed the extend-add-new-menu-approach-3 branch from 9b446d9 to e5a0aae Compare December 7, 2022 13:29
@albarin albarin force-pushed the extend-add-new-menu-approach-3 branch from e5a0aae to 4c12c66 Compare December 7, 2022 13:30
@albarin albarin force-pushed the extend-add-new-menu-approach-3 branch from 1ce8c71 to 102d4c8 Compare December 9, 2022 09:22
@albarin albarin changed the title Allow plugins to extend the list of default templates included in the "Add new" menu Allow plugins to add new items to the "Add new" menu on the Site Editor Dec 9, 2022
@albarin albarin force-pushed the extend-add-new-menu-approach-3 branch from 102d4c8 to 224e96b Compare December 9, 2022 09:35
@ntsekouras ntsekouras added [Feature] Extensibility The ability to extend blocks or the editing experience Needs Design Feedback Needs general design feedback. [Feature] Templates API Related to API powering block template functionality in the Site Editor [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") labels Dec 9, 2022
@jameskoster
Copy link
Contributor

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 #40941.

Copy link
Contributor

@ntsekouras ntsekouras left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work! In general I think this is the time that @WordPress/gutenberg-core should chime in and maybe the @WordPress/gutenberg-design , since adding this is a commitment and there are some new designs about adding new templates. --cc @jorgefilipecosta

@noisysocks
Copy link
Member

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.

@ntsekouras
Copy link
Contributor

ntsekouras commented Jan 11, 2023

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: #41401

@albarin
Copy link
Contributor Author

albarin commented Jan 11, 2023

@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. 🙏

@albarin
Copy link
Contributor Author

albarin commented Jan 27, 2023

@ntsekouras anything else we can do to get more feedback or move this forward? Thanks! 🙌

@ntsekouras
Copy link
Contributor

@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?

@albarin
Copy link
Contributor Author

albarin commented Jan 30, 2023

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 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.
attributes

@dinhtungdu
Copy link
Contributor

dinhtungdu commented Feb 2, 2023

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 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.

@ntsekouras
Copy link
Contributor

ntsekouras commented Feb 2, 2023

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..

@jameskoster
Copy link
Contributor

jameskoster commented Feb 2, 2023

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.

@dinhtungdu
Copy link
Contributor

dinhtungdu commented Feb 2, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Extensibility The ability to extend blocks or the editing experience [Feature] Site Editor Related to the overarching Site Editor (formerly "full site editing") [Feature] Templates API Related to API powering block template functionality in the Site Editor Needs Design Feedback Needs general design feedback.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants