Skip to content

Template Editing: Templates do not recognize theme template parts without a theme attribute #36124

Closed

Description

@kjellr

A theme attribute is not required for template parts to render in the site editor, however it is required in the template editor.

The fallback hierarchy here should be the same, so that the Template editor knows to look for templates in the theme when there's no theme slug provided.

To reproduce:

The following uses Twenty Twenty-Two as an example, but this should be the case with any block theme that uses a template part in its templates.

  1. Use the function from Add an API to define the default template used for the template mode #32771 to map the default Template to one of your theme's existing block templates (one that includes a template part).
/**
 * Add a default block template.
 */
function twentytwentytwo_default_block_template() {
	add_filter( 'block_editor_settings_all',
		function( $settings ) {
			$settings['defaultBlockTemplate'] = file_get_contents( get_theme_file_path( 'block-templates/single.html' ) );
			return $settings;
		}
	);
}
add_action( 'after_setup_theme', 'twentytwentytwo_default_block_template', 0 );
  1. Open a post or page in the editor, and select "New" under the Template panel:

Screen Shot 2021-11-01 at 12 52 41 PM

  1. Note that if your theme uses a theme slug to point to its template part, the template part will appear. Otherwise it will show a "Template not found" error.

Screenshots:

With theme attribute Without theme attribute
Screen Shot 2021-11-01 at 12 43 32 PM Screen Shot 2021-11-01 at 12 42 01 PM

Activity

  1. added
    [Type] Bug
    [Feature] Themes
    [Feature] Template Editing Mode
    on Nov 1, 2021
  2. added
    [Block] Template Part
    on Nov 2, 2021
  3. Mamaduka commented on Nov 2, 2021

    @Mamaduka
    Member

    Here's a workaround for now:

    /**
     * Add a default block template.
     */
    function twentytwentytwo_default_block_template( $settings ) {
    	$template_content                 = file_get_contents( get_theme_file_path( 'block-templates/single.html' ) );
    	$settings['defaultBlockTemplate'] = _gutenberg_inject_theme_attribute_in_content( $template_content );
    
    	return $settings;
    }
    add_filter( 'block_editor_settings_all', 'twentytwentytwo_default_block_template' );
  4. kjellr commented on Nov 12, 2021

    @kjellr
    ContributorAuthor

    @jffng / @carolinan what do you think about that workaround above? If that is generally ok then we can use it for Twenty Twenty-Two. Otherwise I think we need to have this issue prioritized for 5.9.

  5. jffng commented on Nov 12, 2021

    @jffng
    Contributor

    It works, though it looks like _gutenberg_inject_theme_attribute_in_content was renamed to _inject_theme_attribute_in_block_template_content in case you want to test it out.

    what do you think about that workaround above?

    I don't like the idea of adding this to the theme, since the utility function comes from this file which states:

    Temporary compatibility shims for features present in Gutenberg.
    This file should be removed when WordPress 5.9.0 becomes the lowest
    supported version by this plugin.

  6. Mamaduka commented on Nov 12, 2021

    @Mamaduka
    Member

    You should use _inject_theme_attribute_in_block_template_content in theme. But I agree it’s private utility and third parties shouldn’t depend on it.

  7. kjellr commented on Nov 12, 2021

    @kjellr
    ContributorAuthor

    Ok, thanks for weighing in. @noisysocks — what do you think about prioritizing this for 5.9? It's preventing block themes from using theme-supplied template parts in the template editor.

  8. noisysocks commented on Nov 23, 2021

    @noisysocks
    Member

    I'll add it to the board as a general bug to fix (not a blocker).

  9. Mamaduka commented on Jan 5, 2022

    @Mamaduka
    Member

    I'm removing this issue from WP 5.9 project board since RC1 was released last night.

    We can add the defaultTemplate option to the theme.json and handle theme attribute injection internally. Probably task for the next major release since we have a workaround.

  10. youknowriad commented on Jul 22, 2026

    @youknowriad
    Contributor

    Closing as fixed: render_block_core_template_part() now falls back to the active theme when the attribute is absent ($theme = $attributes['theme'] ?? get_stylesheet();), so theme template parts resolve without an explicit theme attribute. Please open a new issue if a resolution gap remains in current template editing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    [Block] Template Part[Feature] Template Editing Mode[Feature] Themes[Type] Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions