PHP-Based Custom Page Templates In Block Themes?
-
I have a site that’s still using a non-block-based theme that I would like to modernize, and it looks like block-based themes are the future. The site uses a number of custom page templates with a lot of PHP that needs to run before the content loads, so I figure that a custom page template is still the way to go rather than using a shortcode.
I’ve been testing with the Twenty Twenty Three theme, and I’ve built the following custom page template for testing purposes:
<?php /** * This is a test custom template for Twenty Twenty Three. * * Template Name: Test Custom Template * */ ?> <!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php $title = get_the_title(); $site_name = get_bloginfo('name'); $page_title = $title . ' - ' . $site_name; ?> <title><?php echo $page_title; ?></title> <?php /* You have to run the do_blocks() between the <head></head> tags in order for WordPress to load the corresponding CSS. */ // Spacer block. $str = '<div style="height:32px" aria-hidden="true" class="wp-block-spacer" ></div>'; $block_spacer = do_blocks($str); // Content block. $block_content = do_blocks( '<!-- wp:group {"layout":{"type":"constrained"}} --> <div class="wp-block-group"> <!-- wp:post-content /--> </div> <!-- /wp:group -->' ); wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <div class="wp-site-blocks"> <header class="wp-block-template-part"> <?php block_header_area(); ?> </header> <main class="wp-block-group"> <div class="wp-block-group has-global-padding is-layout-constrained"> <h1 class="wp-block-post-title"> <?php echo $title; ?> </h1> </div> <?php echo $block_spacer; echo $block_content; echo $block_spacer; ?> <div class="wp-block-group has-global-padding is-layout-constrained"> <div class="entry-content wp-block-post-content is-layout-flow"> <?php echo '<p>Here is some text from PHP.</p>'; ?> </div> </div> <?php echo $block_spacer; ?> </main> <footer class="wp-block-template-part site-footer"> <?php block_footer_area(); ?> </footer> </div> <?php wp_footer(); ?> </body> </html>
The problem is that the elements that should be right-justified, like the navigation menu and footer credits, are not:
I can fix the layout by running
do_blocks()
on the header and footer in a second custom page template like this:<?php /** * This is a test custom template for Twenty Twenty Three. * * Template Name: Test Wrong Header and Footer, Fixed Layout * */ ?> <!DOCTYPE html> <html <?php language_attributes(); ?>> <head> <meta charset="<?php bloginfo('charset'); ?>"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php $title = get_the_title(); $site_name = get_bloginfo('name'); $page_title = $title . ' - ' . $site_name; ?> <title><?php echo $page_title; ?></title> <?php /* You have to run the do_blocks() between the <head></head> tags in order for WordPress to load the corresponding CSS. */ // Header block. $str = '<!-- wp:group {"layout":{"type":"constrained"}} --> <div class="wp-block-group"> <!-- wp:group {"align":"wide","style":{"spacing":{"padding":' . '{"bottom":"var:preset|spacing|40"}}},"layout":{"type":"' . 'flex","justifyContent":"space-between"}} --> <div class="wp-block-group alignwide" style="padding-bottom:' . 'var(--wp--preset--spacing--40)"> <!-- wp:site-title {"level":0} /--> <!-- wp:navigation {"layout":{"type":"flex","setCascading' . 'Properties":true,"justifyContent":"right"}} /--> </div> <!-- /wp:group --> </div> <!-- /wp:group --> '; $block_header = do_blocks($str); // Spacer block. $str = '<div style="height:32px" aria-hidden="true" class="wp-block-spacer" ></div>'; $block_spacer = do_blocks($str); // Content block. $block_content = do_blocks( '<!-- wp:group {"layout":{"type":"constrained"}} --> <div class="wp-block-group"> <!-- wp:post-content /--> </div> <!-- /wp:group -->' ); // Footer block. $str = '<!-- wp:pattern {"slug":"twentytwentythree/footer-default"} /-->'; $block_footer = do_blocks($str); wp_head(); ?> </head> <body <?php body_class(); ?>> <?php wp_body_open(); ?> <div class="wp-site-blocks"> <header class="wp-block-template-part"> <?php echo $block_header; ?> </header> <main class="wp-block-group"> <div class="wp-block-group has-global-padding is-layout-constrained"> <h1 class="wp-block-post-title"> <?php echo $title; ?> </h1> </div> <?php echo $block_spacer; echo $block_content; echo $block_spacer; ?> <div class="wp-block-group has-global-padding is-layout-constrained"> <div class="entry-content wp-block-post-content is-layout-flow"> <?php echo '<p>Here is some text from PHP.</p>'; ?> </div> </div> <?php echo $block_spacer; ?> </main> <footer class="wp-block-template-part site-footer"> <?php echo $block_footer; ?> </footer> </div> <?php wp_footer(); ?> </body> </html>
That fixes the layout (you can see it on the test site via the “Fixed Layout” menu item), but doesn’t display all the header blocks I added and shows the default credits instead of the copyright I added via the Editor:
Adding a PHP-based custom template is so easy with the old, non-block-based themes, but this is giving me fits.
I found this article, which helped get me started, but I haven’t been able to find any documentation for adding PHP-based custom templates for block themes, and the current Page Templates document is still geared to non-block-based themes, it seems.
How do I get WordPress to load both the CSS I need to fix the layout and the correct content for the header and footer?
Or is there a better way to run a PHP-based page in a block theme?
The page I need help with: [log in to see the link]
- The topic ‘PHP-Based Custom Page Templates In Block Themes?’ is closed to new replies.