In WordPress, “Template part has been deleted or is unavailable: header” usually appears right after you switch to a child theme, rename the theme folder or move a site from staging. The parts/header.html file is still there, but the header has disappeared from the front end. With WP_DEBUG on you see that message. Without it you get an empty gap where the header should be.
The cause is the theme attribute on the core/template-part block. When you customise a template in the Site Editor, WordPress saves the template to the database as <!-- wp:template-part {"slug":"header","theme":"old-theme"} /-->. Core only renders a template part when that attribute matches the active theme’s stylesheet slug. So twentytwentyfive stops matching once twentytwentyfive-child is active. The snippet below points those stale references at the active theme, but only when the active theme actually provides a part with that slug.
The snippet
Put it in a small plugin, an mu-plugin or your theme’s functions.php. It also works pasted into a single Scripts Organizer PHP block.
<?php
/**
* Plugin Name: DPC Template Part Theme Fix
* Description: Points template-part blocks saved with an old or parent theme slug at the active theme.
*/
defined( 'ABSPATH' ) || exit;
/**
* Does the active theme (or its parent) provide this template part?
*/
function dpc_tpf_active_theme_has_part( string $slug ): bool {
static $cache = array();
$id = get_stylesheet() . '//' . $slug;
if ( ! array_key_exists( $id, $cache ) ) {
$cache[ $id ] = null !== get_block_template( $id, 'wp_template_part' );
}
return $cache[ $id ];
}
/**
* Should this template-part block be repointed at the active theme?
*/
function dpc_tpf_should_rewrite( array $attrs ): bool {
$theme = $attrs['theme'] ?? '';
$slug = $attrs['slug'] ?? '';
if ( ! is_string( $theme ) || ! is_string( $slug ) || '' === $theme || '' === $slug ) {
return false;
}
if ( get_stylesheet() === $theme || ! dpc_tpf_active_theme_has_part( $slug ) ) {
return false;
}
return (bool) apply_filters( 'dpc_tpf_rewrite_template_part', true, $theme, $slug );
}
/**
* Front end: fix the block right before it renders (templates, patterns, post content).
*/
function dpc_tpf_fix_block_data( array $parsed_block ): array {
if (
'core/template-part' === ( $parsed_block['blockName'] ?? '' )
&& dpc_tpf_should_rewrite( $parsed_block['attrs'] ?? array() )
) {
$parsed_block['attrs']['theme'] = get_stylesheet();
}
return $parsed_block;
}
add_filter( 'render_block_data', 'dpc_tpf_fix_block_data', 10, 1 );
/**
* Walk a block tree and repoint stale template-part blocks.
*/
function dpc_tpf_fix_blocks( array $blocks, bool &$changed ): array {
foreach ( $blocks as $i => $block ) {
if ( 'core/template-part' === ( $block['blockName'] ?? '' ) && dpc_tpf_should_rewrite( $block['attrs'] ?? array() ) ) {
$blocks[ $i ]['attrs']['theme'] = get_stylesheet();
$changed = true;
}
if ( ! empty( $block['innerBlocks'] ) ) {
$blocks[ $i ]['innerBlocks'] = dpc_tpf_fix_blocks( $block['innerBlocks'], $changed );
}
}
return $blocks;
}
/**
* Site Editor and template loader: fix templates saved in the database.
*/
function dpc_tpf_fix_template( $template ) {
static $running = false;
if (
$running
|| ! $template instanceof WP_Block_Template
|| ! is_string( $template->content )
|| ! str_contains( $template->content, 'wp:template-part' )
) {
return $template;
}
$running = true;
$changed = false;
$blocks = dpc_tpf_fix_blocks( parse_blocks( $template->content ), $changed );
if ( $changed ) {
$template->content = serialize_blocks( $blocks );
}
$running = false;
return $template;
}
add_filter( 'get_block_template', 'dpc_tpf_fix_template', 10, 1 );
function dpc_tpf_fix_templates( array $templates ): array {
return array_map( 'dpc_tpf_fix_template', $templates );
}
add_filter( 'get_block_templates', 'dpc_tpf_fix_templates', 10, 1 );
How it works
dpc_tpf_should_rewrite()acts only when a template-part block has athemethat differs fromget_stylesheet()and the active theme can resolve that slug. It checks withget_block_template( 'active-theme//header', 'wp_template_part' ), which also finds parts inherited from a parent theme. A slug that really is missing stays untouched, so you still see the real error.render_block_datafixes the block just before core renders it. That covers templates, patterns and post content that hard-code an old theme slug.- The
get_block_templateandget_block_templatesfilters fix the template content WordPress loads from the database, so the Site Editor sees the corrected slug too. Content is only re-serialised when something actually changed. - A static guard stops recursion, because checking whether a part exists calls
get_block_template()again. Lookups are cached per request.
Customise it
To keep a deliberate cross-theme reference, return false from the dpc_tpf_rewrite_template_part filter. It receives the stored theme slug and the part slug:
add_filter( 'dpc_tpf_rewrite_template_part', function ( bool $rewrite, string $theme, string $slug ): bool {
return 'legacy-footer' === $slug ? false : $rewrite;
}, 10, 3 );
Gotchas
- The message only shows when
WP_DEBUGis true. On a live site the part just renders nothing, so don’t assume it’s fine because there’s no error. See Hide Errors using wp-config file to keep debug output off production. - Edits you made to the header under the old theme don’t follow you, because they’re stored against the old theme’s slug. The snippet points to the active theme’s own version (its file or its own customisation). Copy any changes you need over in the Site Editor.
- If the active theme has no part with that slug, nothing changes. Check that the
slugmatches a file name inparts/(without.html) and that the area is declared undertemplatePartsin theme.json. - After you open an affected template in the Site Editor and save it, the corrected slug is stored permanently. Keep the snippet as a safety net if you import content or patterns from other sites.
- This error is a cousin of the editor’s missing-block placeholder covered in Your Site Doesn’t Include Support for the Block.
Snippet poll. Should this snippet be in the Scripts Organizer library for one-click import? 10 votes put it there.
0 of 10 votes