-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Extending available templates via plugins #41401
Comments
I am getting increased number of questions and support requests about adding templates (and to some degree parts) via plugins, so there is a big interest from developers/ extenders to have this solved. |
Howdy folks! In WooCommerce we have been using custom templates for some time, and we are looking into contributing upstream to standardize a way for plugins to register block templates on WordPress. GoalLike @gigitux highlighted in #42362 (comment), currently any plugin that wants to register custom block templates needs to hook into three filters: So the goal of introducing a new API is:
The APIWhile the current way plugins like WooCommerce are registering templates is via PHP filters, it would be better to have a more explicit API that is solely intended to register templates and template parts. This allows us to decouple the Gutenberg internal logic from the extensibility API shape, which ultimately makes it easier to maintain Gutenberg code, and allows us to build an easier and more flexible API for plugins. Based on the way post types, taxonomies, blocks and block patterns are registered, my suggestion is to create two new functions:
Things this API won't cover
Theme <> plugin templates relation
You might have noticed this means template names are not namespaced, so there is the risk of template names colliding between plugins or between plugins and a theme. We could consider forcing the plugin namespace like Next stepsI would like some feedback on the above topics:
I'm going to ping some folks here (hope you don't mind the noise 🙏 ) as I would really appreciate your feedback. But You are welcome to ignore the ping or pass the baton to somebody else if you prefer. |
@annezazu - who would be the best folks to partner with to contribute here? Woo is willing to contribute the majority of any work that's needed while ensuring we're providing an API that solves the needs of all plugins wanting to extend templates/template parts. |
This is awesome to see contributions here! I know it comes up a fair amount from broader extenders I've spoken to. In terms of who can help with this, it depends on what cycle it might get into. Beta 1 for 6.6 is just over a month away so we're on a tight time table for that. I would be curious if this is something @gziolo can chime in on this and see if folks he works with might be able to help, as there's a lot of API related work happening there (interactivity api, block binding api, HTML api) and I imagine lots can be learned to unblock the work here. |
I'm assuming you meant this only applies to template parts? |
I'm curious what the rationale is for excluding this from the API? |
Sounds like a decent plan, I also wonder why we should exclude the rendering part. It seems inherent to registering templates for me. The other aspect thing on my mind is: is this an API that we can actually use even for "core templates"? Other than that, I think it would be fine to start exploring this Gutenberg plugin-only and get some feedback/iterations on it. |
I was wondering the same thing. I think ideally any API introduced here would also be consumed by core.
This makes me think that the API should be a convenience wrapper over filtering Albert, I know you had mentioned to me you had explored extending With that said, I think one of the open questions for me about explicit attachment to theme is I'm aware I had seen some discussions somewhere about eventually having templates and template parts be agnostic to themes. If true, it's something that might surface as a part of the work here. |
Thanks for all the feedback and comments!
Right, that was a typo. 🤦♂️ I just fixed it.
Good question. My main two reasons to keep it out of the API were:
Having said that, I acknowledge none of them is a real blocker. And I see that adding an optional parameter to the API for this could help make plugin authors' life easier, so they don't need to use hooks. So I'm happy to add it.
I'm not very familiar with how "core templates" are registered, but technically, I don't think there should be anything blocking us from using it, right? I guess the same API could be used for templates and template parts provided by the theme.
Would you mind clarifying what would be the use-case here? You mean allowing themes to override template metadata? While I think that it makes sense that theme template files have priority over plugin ones, so we allow themes to override the content of plugin templates; I'm wary about doing the same with template metadata (template title, post types, template part area...). I think themes shouldn't be able to override that kind of metadata to avoid breaking plugins.
Good call. I may have been overly influenced by the perspective of WooCommerce on this. We currently have the issue that somebody might modify a specific template, assign the current theme's header template part and after switching themes notice the header is broken as it still points to the previous theme template part (woocommerce/woocommerce#42181). That's why I opted for templates being "attached to the theme". However, I acknowledge there might be cases where this might not be needed, like some template parts, or specific templates not used in the frontend (ie: email templates?) or using headers/footers provided by the plugin. Based on that, I guess the best option would be to make it optional via an extra parameter in the API? |
I only had passing familiarity with the This could still be a good option for template options that need to be selected for a custom post type (which plugins might still want to register). But otherwise, likely not for templates attached to specific routes as the default. |
It makes sense, thanks for clarifying, @nerrad! I didn't put a lot of thinking into the implementation yet, as I wanted to keep the discussion limited to the API for now. My initial idea was to create something similar to |
@Aljullu thanks for tackling this! My first thoughts regarding not including the |
@annezazu and @Aljullu, unfortunately, I don't have capacity in the coming weeks and enough expertise to help you with these efforts. However, it looks like @ntsekouras provided all the necessary information to move forward with the issue. |
I updated the API proposal comment above with some changes based on the discussion in this issue and some findings after starting to work on the implementation (very WIP PR: #61577). Basically, I added some extra attributes to the API:
Other considerations:
As usual, all feedback is appreciated. |
I wonder if this should remain a framework thing. I believe having that flag might hurt us later.
This one is also a bit unclear to me. Let's see with implementation, we should probably start conservative. |
Thanks for the feedback, @youknowriad! I would like to get this one right, as we have use cases for both approaches, and whatever the default behavior is will have big implications on how the API is used. To recap, there are two use cases to register templates out there: allowing theme-attached templates and not attached templates. That means, if the user switches themes, should modifications be reset or persist? If we want to keep the API simple and only support one use case by default we can:
The second one feels a bit more flexible to me, but it adds some extra complexity for the use case which I think is more common. And if at some point in the future we want to add support for the
I will keep thinking about this as I continue working on the implementation, but wanted to share some early thoughts as I think this is a key part of the API.
Yeah, I'm fine leaving this one for later. This is something we would like to have in WooCommerce, but I understand it's better to start small and it's not a blocker for the first version of the API. |
We've discussed that we'd want to keep core templates theme specific but at the same time if a user made a modification to a theme template, we'd still surface this alternative template in the UI for users after they switch themes. So I'd say the first option you shared would match that thing and may even address the use-cases of the second alternative. |
Oh, I wasn't aware of that, thanks for the insights, @youknowriad! I will continue exploring the first approach, then. |
@Aljullu @ntsekouras do we have a latest update on this? Curious to see a summary of where things are at :) |
@mtias thanks for checking! We are currently working on the draft PR in #61577. It has received several rounds of feedback and I'm currently working on adding some extra automated tests, after that, I will mark the PR as ready. This should happen either today or early next week. The next steps will be getting an approval and, if I'm understanding the processes correctly, opening an uplift PR in WP core with the same changes. |
There are many plugins that come with their own set of templates (plugins for commerce, newsletters, education, etc) that would need to extend the list of templates available when creating new ones. So this list needs to be easy to filter and extend. From a design perspective, we also need to group them well both for creation flows (in the dropdown) and for management flows.
The text was updated successfully, but these errors were encountered: