Skip to content

Instantly share code, notes, and snippets.

@JodiWarren
Last active July 31, 2018 11:08
Show Gist options
  • Save JodiWarren/fdae60555cb5caddd923cb4bc7c8f0e0 to your computer and use it in GitHub Desktop.
Save JodiWarren/fdae60555cb5caddd923cb4bc7c8f0e0 to your computer and use it in GitHub Desktop.
<?php
class OutputTemplateDetails {
function init() {
add_action( 'all', [$this, 'output_template_data'], 10, 2);
}
function get_template_location($slug, $name) {
$templates = array();
$name = (string) $name;
if ( '' !== $name ) {
$templates[] = "{$slug}-{$name}.php";
}
$templates[] = "{$slug}.php";
return locate_template($templates);
}
function output_template_data($hook, $slug = '', $name = '') {
$PREFIX = 'get_template_part_';
if (strpos($hook, $PREFIX) !== false) {
$strippedName = str_replace($PREFIX, '', $hook);
$templateLocation = $this->get_template_location($slug, $name);
$templateData = [
'location' => $templateLocation,
'name' => $strippedName
];
$jsonTemplate = json_encode($templateData);
echo "<!-- WP-TEMPLATE: ${jsonTemplate} -->";
}
}
}
$outputTemplate = new OutputTemplateDetails();
$outputTemplate->init();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment