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

Add do_action hooks when rendering templates and template parts. ( #32309 ) #32316

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
12 changes: 12 additions & 0 deletions lib/full-site-editing/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ function gutenberg_override_query_template( $template, $type, array $templates )

$block_template = gutenberg_resolve_template( $type, $templates );

/**
* Fires before the template is rendered.
*
* Allows extensions to follow template processing.
* Called before the template is rendered.
*
* @param object $current_template The current template object.
* @param string $type Sanitized filename without extension.
* @param array $templates A list of template candidates, in descending order of priority.
*/
do_action( 'rendering_template', $block_template, $type, $templates );

if ( $block_template ) {
if ( empty( $block_template->content ) && is_user_logged_in() ) {
$_wp_current_template_content =
Expand Down
14 changes: 14 additions & 0 deletions packages/block-library/src/template-part/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,17 @@ function render_block_core_template_part( $attributes ) {

// Run through the actions that are typically taken on the_content.
$seen_ids[ $template_part_id ] = true;
/**
* Fires before/after the template part is rendered.
*
* Allows extensions to follow template part processing.
* Called before and after each template part is rendered.
*
* @param array|null $attributes The template part attributes. Null after rendering.
* @param array $seen_ids The current template part nesting.
*/
do_action( 'rendering_template_part', $attributes, $seen_ids );

$content = do_blocks( $content );
unset( $seen_ids[ $template_part_id ] );
$content = wptexturize( $content );
Expand All @@ -122,6 +133,9 @@ function render_block_core_template_part( $attributes ) {
}
$wrapper_attributes = get_block_wrapper_attributes();

/** This action is documented in template-part.php */
do_action( 'rendering_template_part', null, $seen_ids );

return "<$html_tag $wrapper_attributes>" . str_replace( ']]>', ']]&gt;', $content ) . "</$html_tag>";
}

Expand Down