How to get the name of the file that is used to display the current page?

Say I have a WordPress installation with an "About" page. If I go to http://example.com/about , I know from the WordPress template hierarchy page that I am looking at the page.php theme page.php .

I am wondering if there is a way to show this fact (for debugging a topic) on a page somewhere? How and what function (or code) I call to display the current PHP page, which is used to display the page I'm looking at.

I could do something with $_SERVER['PHP_SELF'] , but I'm looking for a way that I don't need to edit every PHP file. Like something that spits out a list of files that it uses when invoking pages.

+4
source share
4 answers

It can be printed in the Html source code as follows:

 add_action( 'wp_head', 'so_9405896_show_template', 999 ); function so_9405896_show_template() { global $template; echo ' <!-- TEMPLATE = ' . basename($template) .' --> '; } 

Or to simplify the visualization, directly in the content with this:

 add_filter( 'the_content', 'so_9405896_the_content_filter', 20, 1 ); function so_9405896_the_content_filter( $content ) { if( is_admin() || !current_user_can( 'administrator' ) ) return $content; global $template; $the_templ = '<strong style="background-color: #CCC;padding:10px">TEMPLATE = ' . basename( $template ) . '</strong><br />'; $content = sprintf( $the_templ . '%s', $content ); return $content; } 

Result:

output template name to content

+2
source

As far as I saw, there is no built-in option to enable such logging, only for errors.

I'm not sure which editor you use, but the most common text editors allow you to search the entire folder.

I would suggest making a temporary replacement with include and requesting to add an echo to PHP_SELF. Just remember to add a comment or something before the echo so you can easily replace them with nothing when you're done.

0
source

A quick search in the WordPress plugin repository brings up the WordPress Debug Bar template template .

0
source

I just manually enter it into the template, i.e. ARCHIVE.PHP, CATEGORY-1.PHP when I build it. Just remember to delete it as soon as the site appears live. Simple and easy, if not very graceful.

0
source

Source: https://habr.com/ru/post/1397930/


All Articles