Skip to content
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

Allow plugins to manipulate template switching #63284

Open
dinhtungdu opened this issue Jul 9, 2024 · 0 comments
Open

Allow plugins to manipulate template switching #63284

dinhtungdu opened this issue Jul 9, 2024 · 0 comments
Labels
[Type] Enhancement A suggestion for improvement.

Comments

@dinhtungdu
Copy link
Contributor

dinhtungdu commented Jul 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Enhancement A suggestion for improvement.
1 participant