[wp-trac] [WordPress Trac] #56394: Add docs to clarify that `get_page_template()` doesn't work on block themes

WordPress Trac noreply at wordpress.org
Wed Aug 17 01:37:18 UTC 2022


#56394: Add docs to clarify that `get_page_template()` doesn't work on block themes
--------------------------+---------------------
 Reporter:  audrasjb      |       Owner:  (none)
     Type:  defect (bug)  |      Status:  new
 Priority:  normal        |   Milestone:  6.1
Component:  Editor        |     Version:  5.9
 Severity:  normal        |  Resolution:
 Keywords:  needs-patch   |     Focuses:  docs
--------------------------+---------------------

Comment (by costdev):

 I took a deeper look at the implementation.

 TL;DR - Yep, it's a design decision and so a docs-only issue. The snippet
 below is a way I was able to retrieve what I ''expected'' - but it's
 `private`.

 `locate_block_template()`:
 1. Sets `$_wp_current_template_content` to the contents of the block
 template and returns `wp-includes/template-canvas.php`.
 2. `wp-includes/template-canvas.php` calls
 `get_the_block_template_html()`.
 3. `get_the_block_template_html()` makes use of the
 `$_wp_current_template_content` global variable and returns the generated
 template markup.

 So `get_page_template()` does work for block themes in providing a
 template that will be populated with the correct markup. However, it does
 not return the ''actual'' block template being used.

 This seems to achieve the goal of getting the actual `.html` file.
 {{{#!php
 <?php
 function get_the_page_template() {
         $file = _get_block_template_file( 'wp_template',
 get_page_template_slug() );
         var_dump( $file['path'] );
 }
 add_action( 'wp_body_open', 'get_the_page_template' );
 }}}

 Unfortunately, as the `_` signifies `private` access, we couldn't consider
 `_get_block_template_file()` as a recommended public alternative as it's
 subject to change.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/56394#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list