Show the Template Parts Used on a Page
Adds a "Template parts" menu to the front-end admin bar for administrators listing every template part loaded on the page, for both classic themes (get_template_part) and block themes.
Opens WPIDE on
Code
$wpide_template_parts = array();
add_action( 'get_template_part', function ( $slug, $name ) use ( &$wpide_template_parts ) {
$wpide_template_parts[] = $name ? "$slug-$name" : $slug;
}, 10, 2 );
add_filter( 'render_block_core/template-part', function ( $content, $block ) use ( &$wpide_template_parts ) {
if ( ! empty( $block['attrs']['slug'] ) ) {
$wpide_template_parts[] = 'block: ' . $block['attrs']['slug'];
}
return $content;
}, 10, 2 );
// The admin bar renders in the footer, after the template parts have loaded.
add_action( 'admin_bar_menu', function ( $admin_bar ) use ( &$wpide_template_parts ) {
if ( is_admin() || ! current_user_can( 'manage_options' ) ) {
return;
}
$parts = array_values( array_unique( $wpide_template_parts ) );
$admin_bar->add_node( array(
'id' => 'wpide-template-parts',
'title' => 'Template parts (' . count( $parts ) . ')',
) );
foreach ( $parts as $i => $part ) {
$admin_bar->add_node( array(
'id' => 'wpide-tpart-' . $i,
'parent' => 'wpide-template-parts',
'title' => esc_html( $part ),
) );
}
}, 999 );
Notes
Pair it with a template viewer to see the full picture of which files build a page.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.