Skip to main content
Added explanation why class files might occur in the template file list
Source Link

A last modification might be needed in part3) -replacement, if you dont want required includes by plugins. They might call class-files late, and "intercept" during the template output processing.

However, I found it reasonable to leave them visible, as the idea is to track whats been loaded, even if it is not a "template" that rendering output in this stage.

A last modification might be needed in part3) -replacement, if you dont want required includes by plugins. They might call class-files late, and "intercept" during the template output processing.

However, I found it reasonable to leave them visible, as the idea is to track whats been loaded, even if it is not a "template" that rendering output in this stage.

Changed get_home_path() to ABSPATH and added notes about admin-bar issues.
Source Link
echo '<ul><li>'.implode('</li><li>', str_replace(get_home_pathstr_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(get_home_pathstr_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php, but before admin bar renders.

if admin-bar stuff path is showing at the top, or any other file, change the filename template-loader.php in this line of code to: whatever filname you need to break from. Often: class-wp-admin-bar.php

if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:

add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);

priority -5 make shure it loads first. The key is to call get_included_files() at the right moment, otherwise some array-popping needed!

To break this up:

/* The magic point, we need to find its position in the array */
$key$path = get_home_pathstr_replace('\\', '/', ABSPATH);
$key = $path.'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);
$replacement = get_home_path()$path.'wp-content/';
$output = str_replace($replacement, '', $output);
echo '<ul><li>'.implode('</li><li>', str_replace(get_home_path().'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(get_home_path().'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php

To break this up:

/* The magic point, we need to find its position in the array */
$key = get_home_path().'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);
$replacement = get_home_path().'wp-content/';
$output = str_replace($replacement, '', $output);
echo '<ul><li>'.implode('</li><li>', str_replace(str_replace('\\', '/', ABSPATH).'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(str_replace('\\', '/', ABSPATH).'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php, but before admin bar renders.

if admin-bar stuff path is showing at the top, or any other file, change the filename template-loader.php in this line of code to: whatever filname you need to break from. Often: class-wp-admin-bar.php

if you need this in the admin bar, use the right priotity (earliest) to make shure no files are entered at the end of this list. For example:

add_action('admin_bar_menu', 'my_adminbar_template_monitor', -5);

priority -5 make shure it loads first. The key is to call get_included_files() at the right moment, otherwise some array-popping needed!

To break this up:

/* The magic point, we need to find its position in the array */
$path = str_replace('\\', '/', ABSPATH);
$key = $path.'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);
$replacement = $path.'wp-content/';
$output = str_replace($replacement, '', $output);
Source Link

Here you go:

A HTML-list with all template files in use for the current landing page, including all template-parts from plugins, child theme and/ or parent theme combinations, all in one line of code:

echo '<ul><li>'.implode('</li><li>', str_replace(get_home_path().'wp-content/', '', array_slice(str_replace('\\', '/', get_included_files()), (array_search(get_home_path().'wp-includes/template-loader.php', str_replace('\\', '/', get_included_files())) + 1)))).'</li></ul>';

You MAY need to check that your server does not returning dubble slashes at any path. Remember to place this after all template files actually been used, like in footer.php

To break this up:

You can not collect all included template files without PHP backtrace. Superglobals inside template_include wont collect them all. The other way is to "place a marker" in each template file, but if you need to interact with the files first, you hazzle with time and the whole idea.

1) We need to check inside all the files that have been used by current Wordpress request. And they are many! Dont be surprised if you are using 300 files before even your functions.php is registered.

$included_files = str_replace('\\', '/', get_included_files());

We are using the PHP native get_included_files(), converting backslashes to forward slashes to match most of Wordpress returning paths.

2) We are cutting that array from where the template-loader.php is registered. After that, the populated get_included_files() should only have template files populated.

/* The magic point, we need to find its position in the array */
$key = get_home_path().'wp-includes/template-loader.php';
$offset = array_search($key, $included_files);

/* Get rid of the magic point itself in the new created array */
$offset = ($offset + 1);
$output = array_slice($included_files, $offset);

3) Shorten down the results, we dont need the path until theme folder or plugin folder, as templates in use, can be mixed from plugins, theme or child theme folders.

$replacement = get_home_path().'wp-content/';
$output = str_replace($replacement, '', $output);

4) Finally, convert from array to a nice HTML list

$output = '<ul><li>'.implode('</li><li>', $output).'</li></ul>';