Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Template Part Block: Load content from the parent theme if there no template in a child theme #33846

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,30 @@
* @package WordPress
*/

/**
* Gets the contents of a template file from the theme directory.
*
* @param array $attributes The block attributes.
*
* @return string The render or null.
*/
function get_block_template_part_from_theme( $attributes ) {
if ( 0 !== validate_file( $attributes['slug'] ) ) {
return null;
}

$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
if ( ! file_exists( $template_part_file_path ) ) {
$template_part_file_path = get_template_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
}

if ( file_exists( $template_part_file_path ) ) {
return _gutenberg_inject_theme_attribute_in_content( file_get_contents( $template_part_file_path ) );
}

return null;
}

/**
* Renders the `core/template-part` block on the server.
*
Expand Down Expand Up @@ -53,10 +77,7 @@ function render_block_core_template_part( $attributes ) {
} else {
// Else, if the template part was provided by the active theme,
// render the corresponding file content.
$template_part_file_path = get_stylesheet_directory() . '/block-template-parts/' . $attributes['slug'] . '.html';
if ( 0 === validate_file( $attributes['slug'] ) && file_exists( $template_part_file_path ) ) {
$content = _gutenberg_inject_theme_attribute_in_content( file_get_contents( $template_part_file_path ) );
}
$content = get_block_template_part_from_theme( $attributes );
}
}

Expand Down