Make WordPress Core

Opened 4 months ago

Closed 4 months ago

Last modified 4 months ago

#60846 closed defect (bug) (fixed)

When creating a new template, the Choose a pattern screen doesn't honor the `template_hierarchy` filter

Reported by: aljullu's profile aljullu Owned by: jorgefilipecosta's profile jorgefilipecosta
Milestone: 6.6 Priority: normal
Severity: normal Version: 6.1
Component: Editor Keywords: has-patch has-unit-tests
Focuses: template Cc:

Description

Currently, in blocks themes it's possible to use the ${type}_template_hierarchy filter to alter the template hierarchy. However, those filters are not taken into consideration by the Choose a pattern popup screen that appears when creating a new template in the Site Editor, causing a mismatch between the editor and the frontend.

Steps to reproduce

  1. Add this code snippet to your site. You can use the Code Snippets plugin:
<?php
add_filter( 'page_template_hierarchy', function ( $templates ) {
        return ['single'];
} );
  1. Visit any page in the frontend. You will notice the Single template is used, instead of the Page template.
  2. Now, go to Appearance > Editor > Templates > Add New Template > Pages and select any page.
  3. Notice in the Choose a pattern screen the suggested content to start with is from the Page template, instead of the Single template.

Expected result: the Site Editor should follow the filter in the same way the frontend does. So in step 4 the Single template should appear in the Choose a pattern screen, instead of the Page template.

Actual result screenshot

https://github.com/WordPress/gutenberg/assets/3616980/7598c990-1a64-4df3-b509-47d83fc59239

Expected result screenshot

https://github.com/WordPress/gutenberg/assets/3616980/fff09fd9-5600-4969-89f6-dab6523f50de

Real life use case

In WooCommerce, there are Product Category and Product Tag templates. Currently, they fall back to the default WordPress taxonomy templates, but we would like them to fall back to a custom WooCommerce template named Product Catalog.

While this would work in the frontend, the experience would be confusing in the editor, as creating a Product Category archive template wouldn't suggest the same "starting point" from what's seen in the frontend.

Change History (11)

This ticket was mentioned in PR #6322 on WordPress/wordpress-develop by @aljullu.


4 months ago
#1

  • Keywords has-patch has-unit-tests added

## Testing steps

  1. Add this code snippet to your site. You can use the Code Snippets plugin:

{{{PHP
add_filter( 'page_template_hierarchy', function ( $templates ) {

return single?;

} );
}}}

  1. Visit any page in the frontend. You will notice the Single template is used, instead of the Page template.
  2. Now, go to Appearance > Editor > Templates > Add New Template > Pages and select any page.
  3. Verify the suggested content to start with in the Choose a pattern screen is from the Single template, instead of the Page template.

Before | After
--- | ---
https://github.com/WordPress/gutenberg/assets/3616980/fff09fd9-5600-4969-89f6-dab6523f50de

Trac ticket: https://core.trac.wordpress.org/ticket/60846

@aljullu commented on PR #6322:


4 months ago
#2

cc @gziolo in case you have some thoughts on the issue and the approach taken in this PR.

Also cc @ntsekouras as I think you wrote most of the code this PR is based on here: https://github.com/WordPress/gutenberg/pull/42520.

Thanks in advance, folks!

#3 @jorbin
4 months ago

  • Milestone changed from Awaiting Review to Future Release
  • Version changed from trunk to 6.1

This was added in GB13.9 which was a part of 6.1, so adjusting the version accodingly

@gziolo commented on PR #6322:


4 months ago
#4

Also cc @ntsekouras as I think you wrote most of the code this PR is based on here: https://github.com/WordPress/gutenberg/pull/42520.

Nik might have better insights as he worked on the code.

@aljullu commented on PR #6322:


4 months ago
#5

Thanks for taking a look, folks! I updated the PR based on the provided feedback.

I'm wondering though why not start with a PR in Gutenberg repo. This issue it seems will be included in 6.6 and is needed in GB too.

I'm probably missing something from how the Gutenberg → WordPress code transfer works. While I know these functions were first introduced in Gutenberg, I thought they were moved to WordPress core and new contributions/updates should be done here? Or is there another way of approaching this?

---

Note: some PHP tests are failing, but it doesn't seem related to this PR and they are failing in other PRs too.

@ntsekouras commented on PR #6322:


4 months ago
#6

but testing the function on Gutenberg plugin is not trivial as it is not there anymore. I will look into hot to reverse port this PR to Gutenberg and propose a solution today.

Thanks for looking here @jorgefilipecosta and for looking for the GB PR. I think we need to override just one class function that uses this one.

@aljullu commented on PR #6322:


4 months ago
#7

@jorgefilipecosta thanks for bringing this change to Gutenberg! Now that this has been merged there, should I close this PR and the associated trac ticket?

This ticket was mentioned in PR #6370 on WordPress/wordpress-develop by @jorgefilipecosta.


4 months ago
#8

Copy of https://github.com/WordPress/wordpress-develop/pull/6322 just to execute the CI after a rebase.

#9 @jorgefilipecosta
4 months ago

  • Owner set to jorgefilipecosta
  • Resolution set to fixed
  • Status changed from new to closed

In 57944:

Honor template_hierarchy filters when creating a template in the Site Editor.

Currently, in blocks themes it's possible to use the ${type}_template_hierarchy filter to alter the template hierarchy. However, those filters are not taken into consideration by the Choose a pattern popup screen that appears when creating a new template in the Site Editor, causing a mismatch between the editor and the frontend.

Props aljullu, mukesh27, ntsekouras, jorgefilipecosta, gziolo.
Fixes #60846.

@jorgefilipecosta commented on PR #6322:


4 months ago
#10

@jorgefilipecosta thanks for bringing this change to Gutenberg! Now that this has been merged there, should I close this PR and the associated trac ticket?

Yes, I'm closing this PR as it was committed to both Gutenberg and core trunk at https://github.com/WordPress/wordpress-develop/commit/f18c917f6d9aee873513ab8ae531d670a865e638.

#11 @swissspidy
4 months ago

  • Milestone changed from Future Release to 6.6

Note that [57946] was committed as a follow-up to the change here.

Note: See TracTickets for help on using tickets.