Skip to content

Block templates with specific postTypes that have been edited by the user can be selected for any other post type #43557

Closed

Description

@Aljullu

Description

I originally opened this issue in Trac, but moving it here for better visibility.

When a block template provided by a theme has been edited by the user, the postTypes argument is ignored, so the template is available for the wrong post types.

Step-by-step reproduction instructions

  1. With a block theme (I used Twenty Twenty Two), create two templates and add them to theme.json:
{
	"version": 2,
	"customTemplates": [
		....
		{
			"name": "custom-single-post-template",
			"title": "Custom Single Post template",
			"postTypes": [
				"post"
			]
		},
		{
			"name": "custom-single-post-template-not-modified",
			"title": "Custom Single Post template (not modified)",
			"postTypes": [
				"post"
			]
		}
	],
	...
}
  1. Go to Appearance > Editor > Templates and make a modification to Custom Single Post template.
  2. Now, create a new page.
  3. Notice in the Template panel in the sidebar, Custom Single Post template is available while Custom Single Post template (not modified) isn't.

Expected behavior

If the user modifies a template in the Site Editor, that shouldn't affect which post types that template is available for.

Additional info

I think the issue is caused by _build_block_template_result_from_post() not setting a post_types attribute to the returned templates. _build_block_template_result_from_post() is used by get_block_template().

We identified that issue when working on WooCommerce templates. See woocommerce/woocommerce#42630.

Screenshots, screen recording, code snippet

No response

Environment info

No response

Please confirm that you have searched existing issues in the repo.

Yes

Please confirm that you have tested with all plugins deactivated except Gutenberg.

Yes

Activity

  1. added
    [Type] Bug
    [Feature] Templates API
    on Aug 24, 2022
  2. self-assigned this
    on Aug 24, 2022
  3. Aljullu commented on Aug 24, 2022

    @Aljullu
    ContributorAuthor

    PR created in wordpress-develop: WordPress/wordpress-develop#3131

  4. Mamaduka commented on Aug 24, 2022

    @Mamaduka
    Member

    Thanks for the core PR, @Aljullu.

    I had a WIP branch for the plugin, which is slightly different from your patch. So I will try to create PR tomorrow morning.

  5. added
    [Status] In Progress
    on Aug 25, 2022
  6. removed
    [Status] In Progress
    on Sep 2, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Feature] Templates API[Type] Bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions