get_block_templates()
gets all block_templates on the site, but I just want the ones that are loaded for the current page.
I’m trying to check if there is a shortcode inside one of the Gutenberg site-editor templates. It’s not saved in $post_content
or $template
.
This works to check every single one
<code> $templates = get_posts(array(
'post_type' => array('wp_template'),
foreach ($templates as $template_id) {
$template_content = get_post_field('post_content', $template_id);
if (has_shortcode($template_content, 'xxxx')) {
<code> $templates = get_posts(array(
'post_type' => array('wp_template'),
'numberposts' => -1,
'fields' => 'ids'
));
foreach ($templates as $template_id) {
$template_content = get_post_field('post_content', $template_id);
if (has_shortcode($template_content, 'xxxx')) {
styles_and_scripts();
return;
}
}
</code>
$templates = get_posts(array(
'post_type' => array('wp_template'),
'numberposts' => -1,
'fields' => 'ids'
));
foreach ($templates as $template_id) {
$template_content = get_post_field('post_content', $template_id);
if (has_shortcode($template_content, 'xxxx')) {
styles_and_scripts();
return;
}
}
But I only want to check what loads on the page.
I saw query monitor plugin does output the main block template under the tab templates > block template
This is the query monitor code that outputs the correct template and seems to get its id:
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
QM_Util::get_site_editor_url( $data->block_template->id, 'wp_template' ),
esc_html( $data->block_template->id )
<code> echo '<p>';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo self::build_link(
QM_Util::get_site_editor_url( $data->block_template->id, 'wp_template' ),
esc_html( $data->block_template->id )
);
echo '</p>';
}
</code>
echo '<p>';
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo self::build_link(
QM_Util::get_site_editor_url( $data->block_template->id, 'wp_template' ),
esc_html( $data->block_template->id )
);
echo '</p>';
}
I was able to reverse QAs solution to output the template content but this is not ideal:
<code>class Template_Debugger {
protected $template_path = '';
protected $block_template = null;
public function __construct() {
add_action('template_redirect', [$this, 'action_template_redirect']);
add_action('wp_footer', [$this, 'output_template_data']);
public function action_template_redirect() {
add_filter('template_include', [$this, 'filter_template_include'], PHP_INT_MAX);
$this->detect_block_template();
public function filter_template_include($template_path) {
$this->template_path = $template_path;
if ($this->block_template instanceof WP_Block_Template) {
$this->template_path = $this->block_template->theme . '/' . $this->block_template->slug;
public function output_template_data() {
if ($this->template_path) {
$contents = $this->get_template_contents();
echo '<pre>' . esc_html($contents) . '</pre>';
protected function detect_block_template() {
foreach ($this->get_query_template_names() as $template => $conditional) {
if ($this->block_template) {
$get_template = "get_{$template}_template";
if (function_exists($conditional) && function_exists($get_template) && call_user_func($conditional)) {
$filter = str_replace('_', '', $template);
add_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
add_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX, 3);
call_user_func($get_template);
remove_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
remove_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX);
public function filter_template_hierarchy($templates) {
public function filter_template($template, $type, $templates) {
$block_template = $this->wp_resolve_block_template($type, $templates, $template);
$this->block_template = $block_template;
$this->template_path = $block_template->theme . '/' . $block_template->slug;
protected function wp_resolve_block_template($template_type, $template_hierarchy, $fallback_template) {
if (!function_exists('resolve_block_template') || !current_theme_supports('block-templates')) {
return resolve_block_template($template_type, $template_hierarchy, $fallback_template);
protected function get_template_contents() {
if (!$this->template_path) {
if ($this->block_template instanceof WP_Block_Template) {
$template = get_block_template($this->block_template->id, 'wp_template');
return $template ? $template->content : null;
return file_exists($this->template_path) ? file_get_contents($this->template_path) : null;
protected function get_query_template_names() {
'front_page' => 'is_front_page',
'privacy_policy' => 'is_privacy_policy',
'post_type_archive' => 'is_post_type_archive',
'attachment' => 'is_attachment',
'singular' => 'is_singular',
'category' => 'is_category',
'archive' => 'is_archive',
'index' => '__return_true',
<code>class Template_Debugger {
protected $template_path = '';
protected $block_template = null;
public function __construct() {
add_action('template_redirect', [$this, 'action_template_redirect']);
add_action('wp_footer', [$this, 'output_template_data']);
}
public function action_template_redirect() {
add_filter('template_include', [$this, 'filter_template_include'], PHP_INT_MAX);
$this->detect_block_template();
}
public function filter_template_include($template_path) {
$this->template_path = $template_path;
if ($this->block_template instanceof WP_Block_Template) {
$this->template_path = $this->block_template->theme . '/' . $this->block_template->slug;
}
return $template_path;
}
public function output_template_data() {
if ($this->template_path) {
$contents = $this->get_template_contents();
if ($contents) {
echo '<pre>' . esc_html($contents) . '</pre>';
}
}
}
protected function detect_block_template() {
foreach ($this->get_query_template_names() as $template => $conditional) {
if ($this->block_template) {
break;
}
$get_template = "get_{$template}_template";
if (function_exists($conditional) && function_exists($get_template) && call_user_func($conditional)) {
$filter = str_replace('_', '', $template);
add_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
add_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX, 3);
call_user_func($get_template);
remove_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
remove_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX);
}
}
}
public function filter_template_hierarchy($templates) {
return $templates;
}
public function filter_template($template, $type, $templates) {
$block_template = $this->wp_resolve_block_template($type, $templates, $template);
if ($block_template) {
$this->block_template = $block_template;
$this->template_path = $block_template->theme . '/' . $block_template->slug;
}
return $template;
}
protected function wp_resolve_block_template($template_type, $template_hierarchy, $fallback_template) {
if (!function_exists('resolve_block_template') || !current_theme_supports('block-templates')) {
return null;
}
return resolve_block_template($template_type, $template_hierarchy, $fallback_template);
}
protected function get_template_contents() {
if (!$this->template_path) {
return null;
}
if ($this->block_template instanceof WP_Block_Template) {
$template = get_block_template($this->block_template->id, 'wp_template');
return $template ? $template->content : null;
} else {
return file_exists($this->template_path) ? file_get_contents($this->template_path) : null;
}
}
protected function get_query_template_names() {
return [
'embed' => 'is_embed',
'404' => 'is_404',
'search' => 'is_search',
'front_page' => 'is_front_page',
'home' => 'is_home',
'privacy_policy' => 'is_privacy_policy',
'post_type_archive' => 'is_post_type_archive',
'taxonomy' => 'is_tax',
'attachment' => 'is_attachment',
'single' => 'is_single',
'page' => 'is_page',
'singular' => 'is_singular',
'category' => 'is_category',
'tag' => 'is_tag',
'author' => 'is_author',
'date' => 'is_date',
'archive' => 'is_archive',
'index' => '__return_true',
];
}
}
new Template_Debugger();
</code>
class Template_Debugger {
protected $template_path = '';
protected $block_template = null;
public function __construct() {
add_action('template_redirect', [$this, 'action_template_redirect']);
add_action('wp_footer', [$this, 'output_template_data']);
}
public function action_template_redirect() {
add_filter('template_include', [$this, 'filter_template_include'], PHP_INT_MAX);
$this->detect_block_template();
}
public function filter_template_include($template_path) {
$this->template_path = $template_path;
if ($this->block_template instanceof WP_Block_Template) {
$this->template_path = $this->block_template->theme . '/' . $this->block_template->slug;
}
return $template_path;
}
public function output_template_data() {
if ($this->template_path) {
$contents = $this->get_template_contents();
if ($contents) {
echo '<pre>' . esc_html($contents) . '</pre>';
}
}
}
protected function detect_block_template() {
foreach ($this->get_query_template_names() as $template => $conditional) {
if ($this->block_template) {
break;
}
$get_template = "get_{$template}_template";
if (function_exists($conditional) && function_exists($get_template) && call_user_func($conditional)) {
$filter = str_replace('_', '', $template);
add_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
add_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX, 3);
call_user_func($get_template);
remove_filter("{$filter}_template_hierarchy", [$this, 'filter_template_hierarchy'], PHP_INT_MAX);
remove_filter("{$filter}_template", [$this, 'filter_template'], PHP_INT_MAX);
}
}
}
public function filter_template_hierarchy($templates) {
return $templates;
}
public function filter_template($template, $type, $templates) {
$block_template = $this->wp_resolve_block_template($type, $templates, $template);
if ($block_template) {
$this->block_template = $block_template;
$this->template_path = $block_template->theme . '/' . $block_template->slug;
}
return $template;
}
protected function wp_resolve_block_template($template_type, $template_hierarchy, $fallback_template) {
if (!function_exists('resolve_block_template') || !current_theme_supports('block-templates')) {
return null;
}
return resolve_block_template($template_type, $template_hierarchy, $fallback_template);
}
protected function get_template_contents() {
if (!$this->template_path) {
return null;
}
if ($this->block_template instanceof WP_Block_Template) {
$template = get_block_template($this->block_template->id, 'wp_template');
return $template ? $template->content : null;
} else {
return file_exists($this->template_path) ? file_get_contents($this->template_path) : null;
}
}
protected function get_query_template_names() {
return [
'embed' => 'is_embed',
'404' => 'is_404',
'search' => 'is_search',
'front_page' => 'is_front_page',
'home' => 'is_home',
'privacy_policy' => 'is_privacy_policy',
'post_type_archive' => 'is_post_type_archive',
'taxonomy' => 'is_tax',
'attachment' => 'is_attachment',
'single' => 'is_single',
'page' => 'is_page',
'singular' => 'is_singular',
'category' => 'is_category',
'tag' => 'is_tag',
'author' => 'is_author',
'date' => 'is_date',
'archive' => 'is_archive',
'index' => '__return_true',
];
}
}
new Template_Debugger();
$post_content outputs everything easily. How can I get the gutenberg site created page content in a simply way like that instead of the solution I have?
get_block_template()
would work perfectly but I need a simple way to get the template id outside of my monster of a solution.