Add editSite.NewTemplate.defaultTemplateSlugs filter#46270
Add editSite.NewTemplate.defaultTemplateSlugs filter#46270albarin wants to merge 2 commits intoWordPress:trunkfrom albarin:filter-default-template-slugs
editSite.NewTemplate.defaultTemplateSlugs filter#46270Conversation
| setEntityForSuggestions, | ||
| setShowCustomTemplateModal | ||
| ) { | ||
| const DEFAULT_TEMPLATE_SLUGS = applyFilters( |
There was a problem hiding this comment.
I'm generally wary of implementing JS filters because of the potential performance hit, but I don't think this is a hot path so it should be okay.
A question for other reviewers is I'm wondering if it'd be better to get these slugs from the server and filter them server side?
|
Thanks for the PR @albarin! The issue here is how to extend this menu with more templates provided by third party devs. Currently the template creation does quite a few tasks from enhancing menu items dynamically, fallback content and more, and uses the info provided by |
Thanks for your feedback 🙏 Makes sense that it shouldn't be possible to remove anything from function gutenberg_default_template_slugs_settings( $settings ) {
$settings['allowedTemplateSlugs'] = apply_filters('allowed_template_slugs', []);
return $settings;
}
add_filter( 'block_editor_settings_all', 'gutenberg_default_template_slugs_settings');and then on the export const useAllowedTemplatesSlugs = ( editSiteStore ) => {
return useSelect(
( select ) =>
select( editSiteStore ).getSettings().allowedTemplateSlugs,
[]
);
};const allowedTemplateSlugs = useAllowedTemplatesSlugs( editSiteStore );
...
const missingDefaultTemplates = ( defaultTemplateTypes || [] ).filter(
( template ) =>
[ ...DEFAULT_TEMPLATE_SLUGS, ...allowedTemplateSlugs ].includes(
template.slug
) && ! existingTemplateSlugs.includes( template.slug )
);Thanks again! 🙌 |
|
Yes, something like your above comment would be one approach. We'd need to explore a bit here though with some PR(s). Maybe another approach would to have a value that also lives in store that keeps the extra template types, and introduce an API like
I'm not sure if there is an issue about this, but if not could you create one? Thanks! |
What?
This PR adds the new
editSite.NewTemplate.defaultTemplateSlugsfilter which will allow plugins to extend the list of default template slugs.Why?
Currently, it is possible to extend the list of default templates via the
default_template_typeshook, but since there's no way to extend theDEFAULT_TEMPLATE_SLUGSin Gutenberg, the templates are filtered out from theAdd newdropdown menu.For example, via the WooCommerce plugin, we want to make a block template available in the
Add menuuser interface that will apply to all product attributes pages, which wouldn't be possible right now.How?
This PRs is introducing a new filter that allows other plugins to modify the
DEFAULT_TEMPLATE_SLUGSlist.Testing Instructions
Site Editor>Browse all templates(/wp-admin/site-editor.php?postType=wp_template)Add newbutton and make sure you see an item in the dropdown for theMy new templatetemplate.Screenshots