REST API & Developer PHP Beginner

Show Current Template Filename in the Admin Bar

Display the name of the PHP template file being used to render each page in the WordPress admin bar, making it instant to identify which template file to edit.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 14 lines
add_action( 'admin_bar_menu', function ( WP_Admin_Bar $bar ): void {
	global $template;
	if ( is_admin() || ! current_user_can( 'manage_options' ) || empty( $template ) ) {
		return;
	}

	$file = ltrim( str_replace( [ get_stylesheet_directory(), get_template_directory(), WP_PLUGIN_DIR ], '', $template ), '/' );

	$bar->add_node( [
		'id'    => 'nahnu-current-template',
		'title' => esc_html( 'Template: ' . $file ),
		'meta'  => [ 'title' => esc_attr( $template ) ],
	] );
}, 999 );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

When you’re working on a theme, the first question is often “which template file is WordPress using for this page?” This snippet shows the answer in the admin bar for administrators, on every front-end page.

How it works

WordPress stores the path of the template it chose in the global $template variable. The snippet adds an admin bar item on admin_bar_menu showing that path, shortened to be relative to your theme or plugins folder. Hover over it to see the full path.

The previous version linked the item to the Theme File Editor. That editor is turned off on many sites, including any that use our “Disable the Theme and Plugin File Editor” snippet, so the link led to an error. The file name is also escaped now before it’s shown.

Block themes

On block themes, $template points to WordPress’s template-canvas.php, because the real templates are stored as blocks. The Site Editor shows which template a page uses on those themes.

Things to watch

Only administrators see the item, and only on the front end. It’s safe to leave on a live site, but you’ll usually only want it during development.

How to test it

While logged in as an administrator, open a single post. The admin bar should show something like “Template: single.php”.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only