Skip to content

Template activation: use "old" endpoint in the post editors #72586

@ellatrix

Description

@ellatrix

This is for both the post editor and the page editor in the site editor, and is about picking a template to set to the post object (template property). This property only accepts custom templates slugs. This makes it so that it's impossible to select a template ID of a template that is not active as the template to use for a specific post. For this reason I think it may be better to revert to using the old template endpoint which will only return slugs, instead of the convoluted logic we have now:

function useTemplates( postType ) {
// To do: create a new selector to checks if templates exist at all instead
// of and unbound request. In the modal, the user templates should be
// paginated and we should not make an unbound request.
const { defaultTemplateTypes, registeredTemplates, userTemplates } =
useSelect(
( select ) => {
return {
defaultTemplateTypes:
select( coreStore ).getCurrentTheme()
?.default_template_types,
registeredTemplates: select( coreStore ).getEntityRecords(
'postType',
'wp_registered_template',
{
per_page: -1,
post_type: postType,
}
),
userTemplates: select( coreStore ).getEntityRecords(
'postType',
'wp_template',
{ per_page: -1, combinedTemplates: false }
),
};
},
[ postType ]
);
return useMemo( () => {
if (
! defaultTemplateTypes ||
! registeredTemplates ||
! userTemplates
) {
return [];
}
return [
...registeredTemplates,
...userTemplates.filter(
( template ) =>
// Only give "custom" templates as an option, which
// means the is_wp_suggestion meta field is not set and
// the slug is not found in the default template types.
// https://github.com/WordPress/wordpress-develop/blob/97382397b2bd7c85aef6d4cd1c10bafd397957fc/src/wp-includes/block-template-utils.php#L858-L867
! template.meta.is_wp_suggestion &&
! defaultTemplateTypes.find(
( type ) => type.slug === template.slug
)
),
];
}, [ registeredTemplates, userTemplates, defaultTemplateTypes ] );
}

This would make sure that template picking for the post editors works exactly as before.

In the long term, we need to find a way for the template property to accept IDs.

Related: #67125 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Templates APIRelated to API powering block template functionality in the Site Editor

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions