close
Skip to content

Allow plugins to manipulate template switching #63284

@dinhtungdu

Description

@dinhtungdu

What problem does this address?

Currently, the useAllowSwitchingTemplates  is used to disable template swapping and creating for static home and blog pages. This prevents users from accidentally creating or swapping templates but still gives them information about the connected template and a path to edit it from the post editor, which is a nice UX.

Other plugins can benefit from this hook. For example, WooCommerce has Product Catalog, Cart, and Checkout page that uses fixed templates like the blog page.

export function useAllowSwitchingTemplates() {
const { postType, postId } = useEditedPostContext();
return useSelect(
( select ) => {
const { getEntityRecord, getEntityRecords } = select( coreStore );
const siteSettings = getEntityRecord( 'root', 'site' );
const templates = getEntityRecords( 'postType', 'wp_template', {
per_page: -1,
} );
const isPostsPage = +postId === siteSettings?.page_for_posts;
// If current page is set front page or posts page, we also need
// to check if the current theme has a template for it. If not
const isFrontPage =
postType === 'page' &&
+postId === siteSettings?.page_on_front &&
templates?.some( ( { slug } ) => slug === 'front-page' );
return ! isPostsPage && ! isFrontPage;
},
[ postId, postType ]
);
}

The current hook only supports static home and blog page.

What is your proposed solution?

It'd be awesome to have a mechanism enabling plugins to manipulate the template-switching ability of the current editing page.

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Feature] Templates APIRelated to API powering block template functionality in the Site Editor[Type] EnhancementA suggestion for improvement.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions