diff --git a/lib/template-loader.php b/lib/template-loader.php index 82a86e7a539e3..9b8cea2a7fd6f 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -176,7 +176,9 @@ function create_auto_draft_for_template_part_block( $block ) { } if ( $template_part_file_path ) { - $file_contents = file_get_contents( $template_part_file_path ); + ob_start(); + include $template_part_file_path; + $file_contents = ob_get_clean(); if ( $template_part_post && $template_part_post->post_content === $file_contents ) { $template_part_id = $template_part_post->ID; } else { @@ -214,7 +216,7 @@ function create_auto_draft_for_template_part_block( $block ) { * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority. * @return null|array { * @type WP_Post|null template_post A template post object, or null if none could be found. - * @type int[] A list of template parts IDs for the template. + * @type int[] template_part_ids A list of template parts IDs for the template. * } */ function gutenberg_find_template_post_and_parts( $template_type, $template_hierarchy = array() ) { @@ -283,8 +285,10 @@ function gutenberg_find_template_post_and_parts( $template_type, $template_hiera // If there is, use it instead. if ( isset( $higher_priority_block_template_path ) ) { - $post_name = basename( $higher_priority_block_template_path, '.html' ); - $file_contents = file_get_contents( $higher_priority_block_template_path ); + $post_name = basename( $higher_priority_block_template_path, '.html' ); + ob_start(); + include $higher_priority_block_template_path; + $file_contents = ob_get_clean(); $current_template_post = array( 'post_content' => $file_contents, 'post_title' => $post_name, diff --git a/lib/template-parts.php b/lib/template-parts.php index b09bd83ba0b86..21f7c9438a1bb 100644 --- a/lib/template-parts.php +++ b/lib/template-parts.php @@ -236,7 +236,9 @@ function get_template_part_paths( $base_directory ) { } // Build and save each template part. foreach ( $template_part_files as $template_part_file ) { - $content = file_get_contents( $template_part_file ); + ob_start(); + include $template_part_file; + $content = ob_get_clean(); // Infer slug from filepath. $slug = substr( $template_part_file, diff --git a/packages/block-library/src/template-part/index.php b/packages/block-library/src/template-part/index.php index 29e3668170f2f..91c3027901717 100644 --- a/packages/block-library/src/template-part/index.php +++ b/packages/block-library/src/template-part/index.php @@ -41,7 +41,9 @@ function render_block_core_template_part( $attributes ) { // render the corresponding file content. $template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html'; if ( 0 === validate_file( $template_part_file_path ) && file_exists( $template_part_file_path ) ) { - $content = file_get_contents( $template_part_file_path ); + ob_start(); + include $template_part_file_path; + $content = ob_get_clean(); } } }