Get WordPress block templates content similar to $post_content

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<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>
<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.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật