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
Add editSite.NewTemplate.defaultTemplateSlugs
filter
#46270
Conversation
@@ -263,6 +249,24 @@ function useMissingTemplates( | |||
setEntityForSuggestions, | |||
setShowCustomTemplateModal | |||
) { | |||
const DEFAULT_TEMPLATE_SLUGS = applyFilters( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.defaultTemplateSlugs
filter 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_types
hook, but since there's no way to extend theDEFAULT_TEMPLATE_SLUGS
in Gutenberg, the templates are filtered out from theAdd new
dropdown 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 that allows other plugins to modify the
DEFAULT_TEMPLATE_SLUGS
list.Testing Instructions
Site Editor
>Browse all templates
(/wp-admin/site-editor.php?postType=wp_template
)Add new
button and make sure you see an item in the dropdown for theMy new template
template.Screenshots