Make WordPress Core

Opened 21 months ago

Last modified 2 months ago

#56762 new defect (bug)

PHP file being ignored in block theme hierarchy

Reported by: ryanpluckrose's profile ryanpluckrose Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 6.0.2
Component: Themes Keywords: reporter-feedback close
Focuses: template Cc:

Description

Hi

I've tried the following in WP 6.0 and 6.1 as well as the TwentyTwentyTwo and TwentyTwentyThree themes.

Issue:
When using the "Default Template" option on a Page, the page.php file is ignored but page.html will work. You will see in admin that it is set to Index as it is ignoring the page.php file but if you have both .html and .php the .html will take precedence.

Admin + File structure: https://monosnap.com/file/0IwMr0nBwtXDkd8SEIb0QjUGTn7Qsb

It looks like the problem comes from the code below in this file wp-includes/block-template-utils.php line 313

    $template_slug      = substr(
    $template_file,
    // Starting position of slug.
    strpos( $template_file, $template_base_path . DIRECTORY_SEPARATOR ) + 1 + strlen( $template_base_path ),
    // Subtract ending '.html'.
    -5
);

I'm not certain but I also found this in the Gutenberg side and wondering if it didn't make it across (unless I'm mistaken).

https://github.com/WordPress/gutenberg/pull/31478/files

It's been driving me mad!

Change History (13)

#1 @ryanpluckrose
21 months ago

and the title should obviously be PHP file being ignored in the block theme hierarchy but I'm not sure how to edit it now ....

#2 @SergeyBiryukov
21 months ago

  • Summary changed from PHP file being ignore in block theme hierarchy to PHP file being ignored in block theme hierarchy

Hi there, welcome to WordPress Trac! Thanks for the report.

Updating the title per comment:1.

#3 @ryanpluckrose
21 months ago

I forgot to mention that this works fine if you actually select the template you want i.e. change from Default Template to Page

#4 @ryanpluckrose
21 months ago

I'm not sure if this is directly related but I've now discovered that I can't use Site Editor while this PHP file exists, I just get a white page with no errors.

Swapping back to a html file makes it work.

Last edited 21 months ago by ryanpluckrose (previous) (diff)

#5 @ryanpluckrose
20 months ago

This seems to be a problem everywhere I turn, I'm having the same issue with template-parts

If I have a custom template part that is a html file, it works fine but if I change it to php, I get Template part has been deleted or is unavailable

Is no one else experiencing this?

#6 @willecs
20 months ago

I found that putting the php template into the root of the theme directory, it then works, however this does not work for php template parts.

I added a filter to 'theme_file_path' to make this work.

<?php
function test_theme_file_path($path, $file)
{
    if (file_exists($path)) {
        return $path;
    }

    $file = str_replace('.html', '.php', $file);
    $file = ltrim( $file, '/' );

    if ( empty( $file ) ) {
        $path = get_stylesheet_directory();
    } elseif ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
        $path = get_stylesheet_directory() . '/' . $file;
    } else {
        $path = get_template_directory() . '/' . $file;
    }

    return $path;
}

add_filter('theme_file_path', 'test_theme_file_path', 10, 2);

#7 @poena
20 months ago

It is correct that the PHP (page-)template needs to be in the root directory, @ryanpluckrose did you try moving it out of the templates folder?

I don't believe that block template parts as PHP files are supported by Gutenberg or core, I have not heard of it before, and I would like to learn what the use case is?
I suggest opening a new issue for this specifically, but in the Gutenberg GitHub repository.

#8 follow-up: @psykro
20 months ago

@ryanpluckrose hey there. Could I confirm something, does this ticket related to your question about adding a post meta field to a template?

https://wordpress.org/support/topic/how-to-add-post_meta-to-a-block-theme-html-file/#post-16075706

As @poena has pointed out, PHP files will not work inside the templates or parts directories of a block theme. If you need PHP functionality, you might have to add that to a pattern, which is included in the template or template part.

This theme developer handbook explains how to create and use patterns

https://developer.wordpress.org/themes/advanced-topics/block-patterns/

#9 @ryanpluckrose
20 months ago

Yeah I'm currently working with page.php in the root to solve the issue, I didn't realise at the time it was by design.

I had assumed that template-parts would fallback in the same way templates do as above but if that's by design then I guess it's not a bug.

https://fullsiteediting.com/ has been very helpful for getting me this far though, so thank you!

Because page.php is used I now get a white page using Appearance > Site Editor, I know that it's not supposed to work with PHP files but is that the intended result?

How should the user edit the nav menu in this context, do I need to enable appearance > menus? I thought this method was now legacy.

For reference, this is my use case:

  • On pages, users will have the option to select a STYLE dropdown, this will update the frontend based on the styles json files e.g. styles/swiss.json or styles/blue.json
  • If one of these is selected, as well as the styles, the menu will change so i needed an IF statement around the nav, this is where I hit trouble with PHP template parts as Will mentions above.

I've currently changed my implementation so that header.html will not be used but instead I'll call a smaller nav.html and put the rest of the header inside a php file called from page.php

<!doctype html>
<html <?php language_attributes(); ?>>
    <head>
        <meta charset="<?php bloginfo( 'charset' ); ?>">
        <?php wp_head(); ?>
    </head>
    <body <?php body_class(); ?>>
        <?php wp_body_open(); ?>
        <div class="wp-site-blocks">
            <?php get_template_part( 'header' ) ?>
            <?php get_template_part( 'content' ) ?>
            <footer class="wp-block-template-part site-footer">
                <?php block_footer_area(); ?>
            </footer>
        </div>
        <?php wp_footer(); ?>
    </body>
</html>

snippet of my header.php (further down I'll call my nav.html part)

        <?php if ( isset( $meta_page_style ) && $meta_page_style !== 'default' ): ?>
                <?php // Alternative header when top nav is removed ?>
                <?= $part_header_alt ?>
        <?php else: ?>
                <?php block_header_area(); ?>
        <?php endif; ?>

    <div class="wp-block-group is-layout-constrained">
        <div class="is-content-justification-left is-nowrap is-layout-flex wp-block-group alignwide">
                        <?= do_blocks( '<!-- wp:site-logo {"width":218,"shouldSyncIcon":true,"className":"is-style-default"} /-->' ) ?>
            <div class="wp-header-spacer"></div>

Thank you!

#10 in reply to: ↑ 8 @ryanpluckrose
20 months ago

Ah yes it is!

Mixed with that post and the explainer above hopefully you can see what I'm trying to achieve.

I'll have a read of that handbook link, thanks!

Replying to psykro:

@ryanpluckrose hey there. Could I confirm something, does this ticket related to your question about adding a post meta field to a template?

https://wordpress.org/support/topic/how-to-add-post_meta-to-a-block-theme-html-file/#post-16075706

As @poena has pointed out, PHP files will not work inside the templates or parts directories of a block theme. If you need PHP functionality, you might have to add that to a pattern, which is included in the template or template part.

This theme developer handbook explains how to create and use patterns

https://developer.wordpress.org/themes/advanced-topics/block-patterns/

#11 @ryanpluckrose
20 months ago

It seems block-patterns are the answer to all of my questions!

I will change my code to use them instead.

Thank you!

#12 @ryanpluckrose
20 months ago

One further question on block patterns, how can I get the post?

I want to be able to use get_post_meta()

#13 @poena
2 months ago

  • Keywords reporter-feedback close added; needs-patch removed
  • Severity changed from major to normal

Hi @ryanpluckrose
Are there any feature requests or bugs left that are actionable in this ticket, or can the ticket be closed?

Kindly remember that the Trac ticket system is not for support requests.

Note: See TracTickets for help on using tickets.