Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.