From f7895d2f54c4b1ec65644e49074d27b10d6dfd1e Mon Sep 17 00:00:00 2001 From: Keith Solomon Date: Tue, 11 Nov 2025 11:14:18 -0600 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8feature:=20Add=20script=20to=20show=20?= =?UTF-8?q?template=20used=20comment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/show-template.php | 158 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 lib/show-template.php diff --git a/lib/show-template.php b/lib/show-template.php new file mode 100644 index 0000000..e861303 --- /dev/null +++ b/lib/show-template.php @@ -0,0 +1,158 @@ +template = $template; + } + } elseif ( is_search() ) { + $template = get_search_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_tax() ) { + $template = get_taxonomy_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_home() ) { + $template = get_home_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_front_page() ) { + $template = get_front_page_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_attachment() ) { + $template = get_attachment_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_single() ) { + $template = get_single_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_page() ) { + $template = get_page_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_category() ) { + $template = get_category_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_tag() ) { + $template = get_tag_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_author() ) { + $template = get_author_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_date() ) { + $template = get_date_template(); + if ( $template ) { + $this->template = $template; + } + } elseif ( is_archive() ) { + $template = get_archive_template(); + if ( $template ) { + $this->template = $template; + } + } else { + $this->template = function_exists( 'get_index_template' ) ? get_index_template() : get_template_directory() . '/index.php'; + } + $this->template = apply_filters( 'template_include', $this->template ); + // Hook into the footer so we can echo the active template + add_action( 'wp_footer', array( &$this, 'show_template' ), 100 ); + } + + /** + * Echo the active template to the footer + * Try to catch when a plugin or otherwise hooks template_redirect to include a different template + * + * @since 1.0.0 + */ + public function show_template() { + $fudge = false; + + foreach ( debug_backtrace() as $trace ) { // phpcs:ignore + switch ( $trace['function'] ) { + case 'wp_footer': + $wp_footer = $trace['file']; + break; + case 'get_footer': + $get_footer = $trace['file']; + break; + } + } + + $fudge = isset( $get_footer ) ? $get_footer : $wp_footer; + + if ( $fudge === $this->template || $fudge === false ) { + echo wp_kses_post( "\n" ); + } else { + echo esc_html( "\n" ); + } + } +} + +$ShowTemplate = new ShowTemplate();