Your support helps keep this blog running! Secure payments via Paypal and Stripe.
When working with custom WordPress templates, it’s helpful to know which template files are being loaded on the current page. This allows you to easily identify where to add your custom code.
Snippet code to display WordPress’s current template
Here is a code snippet to help you debug the WordPress template hierarchy. Add this to your functions.php or a custom plugin.
// Show the main template file
add_filter('template_include', function ($template) {
if (is_user_logged_in() && current_user_can('administrator')) {
add_action('wp_footer', function () use ($template) {
echo '<div style="position:fixed;bottom:0;left:0;background:#fff;color:#000;padding:10px;z-index:9999;font-size:12px;border-top:1px solid #ccc;border-right:1px solid #ccc">';
echo '<strong>Main Template:</strong><br>' . esc_html(str_replace(ABSPATH, '', $template)) . '<br>';
echo '<strong>Template Parts:</strong><br>';
global $template_parts_logged;
if (!empty($template_parts_logged)) {
foreach ($template_parts_logged as $part) {
echo esc_html(str_replace(ABSPATH, '', $part)) . '<br>';
}
} else {
echo 'No template parts tracked.';
}
echo '</div>';
});
}
return $template;
});
// Hook into get_template_part() to track parts
function track_template_part($slug, $name = null, $args = []) {
global $template_parts_logged;
$template = locate_template([
"{$slug}-{$name}.php",
"{$slug}.php"
]);
if ($template) {
$template_parts_logged[] = $template;
}
}
add_action('get_template_part', 'track_template_part', 10, 3);
How It Works
- Shows the main template file used (e.g.,
page.php,single.php,archive-product.php, etc.). - Tracks all files used via
get_template_part()(e.g., headers, footers, loops). - Only displays info to logged-in administrators in the frontend footer.
- Outputs relative paths for better readability.
When you are not using this snippet code, ensure to disable it.
Your support helps keep this blog running! Secure payments via Paypal and Stripe.

