| @@ -16,33 +16,54 @@ | ||
| 16 | 16 | |
| 17 | 17 | defined( 'ABSPATH' ) || exit; |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | - * Safely render a block template part with fallback. | |
| 20 | + * Safely get rendered block template part HTML with fallback. | |
| 21 | 21 | * |
| 22 | + * In block (FSE) themes, template parts must be rendered before wp_head() so | |
| 23 | + * that Gutenberg layout styles (flex, space-between, gap, constrained layout, etc.) | |
| 24 | + * are registered in the Style Engine and enqueued via wp_enqueue_stored_styles() in <head>. | |
| 25 | + * | |
| 22 | 26 | * @param string $part The template part name (e.g., 'header', 'footer'). |
| 27 | + * @param string $tag_name Optional HTML tag name (e.g., 'header', 'footer'). | |
| 28 | + * @return string Rendered HTML. | |
| 23 | 29 | */ |
| 24 | -function forumax_render_block_template_part( $part ) { | |
| 25 | - // Check if block_template_part function exists (WP 5.9+) | |
| 26 | - if ( ! function_exists( 'block_template_part' ) ) { | |
| 27 | - return; | |
| 30 | +function forumax_get_block_template_part_html( $part, $tag_name = '' ) { | |
| 31 | + if ( empty( $tag_name ) ) { | |
| 32 | + $tag_name = ( 'header' === $part || 'footer' === $part ) ? $part : 'div'; | |
| 28 | 33 | } |
| 29 | 34 | |
| 30 | - // Check if the template part exists in the theme | |
| 31 | - $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); | |
| 35 | + if ( function_exists( 'get_block_template' ) ) { | |
| 36 | + $template_part = get_block_template( get_stylesheet() . '//' . $part, 'wp_template_part' ); | |
| 37 | + if ( ! $template_part && get_stylesheet() !== get_template() ) { | |
| 38 | + $template_part = get_block_template( get_template() . '//' . $part, 'wp_template_part' ); | |
| 39 | + } | |
| 32 | 40 | |
| 33 | - if ( $template_part ) { | |
| 34 | - block_template_part( $part ); | |
| 35 | - } elseif ( 'header' === $part ) { | |
| 36 | - // Minimal fallback header | |
| 41 | + if ( $template_part && function_exists( 'do_blocks' ) ) { | |
| 42 | + return do_blocks( '<!-- wp:template-part {"slug":"' . esc_attr( $part ) . '","tagName":"' . esc_attr( $tag_name ) . '"} /-->' ); | |
| 43 | + } | |
| 44 | + } | |
| 45 | + | |
| 46 | + // Fallback if no block template part found. | |
| 47 | + ob_start(); | |
| 48 | + if ( 'header' === $part ) { | |
| 37 | 49 | forumax_minimal_header(); |
| 38 | 50 | } elseif ( 'footer' === $part ) { |
| 39 | - // Minimal fallback footer | |
| 40 | 51 | forumax_minimal_footer(); |
| 41 | 52 | } |
| 53 | + return ob_get_clean(); | |
| 42 | 54 | } |
| 43 | 55 | |
| 44 | 56 | /** |
| 57 | + * Safely render a block template part with fallback. | |
| 58 | + * | |
| 59 | + * @param string $part The template part name (e.g., 'header', 'footer'). | |
| 60 | + */ | |
| 61 | +function forumax_render_block_template_part( $part ) { | |
| 62 | + echo forumax_get_block_template_part_html( $part ); | |
| 63 | +} | |
| 64 | + | |
| 65 | +/** | |
| 45 | 66 | * Minimal header fallback when theme has no header template part. |
| 46 | 67 | */ |
| 47 | 68 | function forumax_minimal_header() { |
| 48 | 69 | ?> |
| @@ -67,8 +88,12 @@ | ||
| 67 | 88 | </div> |
| 68 | 89 | </footer> |
| 69 | 90 | <?php |
| 70 | 91 | } |
| 92 | + | |
| 93 | +// Pre-render header and footer before wp_head() so Gutenberg collects block support layout styles. | |
| 94 | +$forumax_header_html = forumax_get_block_template_part_html( 'header', 'header' ); | |
| 95 | +$forumax_footer_html = forumax_get_block_template_part_html( 'footer', 'footer' ); | |
| 71 | 96 | ?><!DOCTYPE html> |
| 72 | 97 | <html <?php language_attributes(); ?>> |
| 73 | 98 | <head> |
| 74 | 99 | <meta charset="<?php bloginfo( 'charset' ); ?>"> |
| @@ -77,9 +102,9 @@ | ||
| 77 | 102 | </head> |
| 78 | 103 | <body <?php body_class(); ?>> |
| 79 | 104 | <?php wp_body_open(); ?> |
| 80 | 105 | |
| 81 | -<?php forumax_render_block_template_part( 'header' ); ?> | |
| 106 | +<?php echo $forumax_header_html; ?> | |
| 82 | 107 | |
| 83 | 108 | <main id="frmx-bbpress-main" class="frmx-bbpress-page"> |
| 84 | 109 | <?php |
| 85 | 110 | // Check if bbPress has content to display |
| @@ -98,9 +123,9 @@ | ||
| 98 | 123 | } |
| 99 | 124 | ?> |
| 100 | 125 | </main> |
| 101 | 126 | |
| 102 | -<?php forumax_render_block_template_part( 'footer' ); ?> | |
| 127 | +<?php echo $forumax_footer_html; ?> | |
| 103 | 128 | |
| 104 | 129 | <?php wp_footer(); ?> |
| 105 | 130 | </body> |
| 106 | 131 | </html> |