Fix subdirectory support in site editor template parts #49100
Labels
[Block] Template Part
Affects the Template Parts Block
[Type] Bug
An existing feature does not function as intended
Description
I have a series of template parts in subdirectories of my theme's
parts/
:This works fine at first glance. Everything loads as expected in the site editor and on the front-end with the default markup that I've entered into each of those parts.
When I try to save one of these template parts from the site editor, a
POST
request is made to thewp/v2/template-parts/
endpoint like:https://site.test/wp-json/wp/v2/template-parts/theme-name//parts/subdir1/part-name?_locale=user
With body JSON like:
The response says that it saves, but no changes are reflected when I refresh the page or attempt to view the changes on the front-end.
When I look in the site's template parts interface, I see a new part added for:
theme-name//parts/subdir//part-name
.When I look at the response to the
POST
request, I see an id oftheme-name//parts/subdir//part-name
, different from that of the ID that was submitted.This appears to be caused by the REST controller's
_sanitize_template_id()
method, which "relies on the template ID format {theme_name}//{template_slug} and the fact that slugs cannot contain slashes".From poking around at other issues, it seems like the intent of something like #25063 was to support subdirectories in
parts/
, but then #36881 may have reversed that through the introduction of_sanitize_template_id()
.I would propose that supporting subdirectories in
parts/
is necessary (and mostly supported already?), as staring at a wall of filenames without organization is no fun. :)If it is not supported, then things like
get_template_part()
should be adjusted to match expectations.Step-by-step reproduction instructions
OR
Pass
theme-name//parts/subdir/footer
to_sanitize_template_id()
:theme-name//parts/subdir/footer
theme-name//parts/subdir//footer
Screenshots, screen recording, code snippet
No response
Environment info
WordPress 6.1, Gutenberg latest, active or not
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
The text was updated successfully, but these errors were encountered: