For anyone interested, with the suggestion from @TomJNowell I managed to retry using the render_block
filter and simplify the solution to the problem:
Callback function (replacing example_filter_the_page_content()
from my initial post):
/**
* Callback function used to skip intro section block from rendering.
*
* To be used as callback for the `render_block` filter.
*/
function example_filter_render_block_intro( $block_content, $block ) {
if ( is_page() && is_singular() && in_the_loop() && is_main_query() ) {
if ( 'core/group' === $block['blockName'] &&
! empty( $block['attrs']['className'] ) &&
preg_match( '/\bis-style-intro-section\b/', $block['attrs']['className'] ) ) {
$block_content="";
}
}
return $block_content;
}
And then calling add_filter
after rendering the intro, right before calling the the_content()
function to render the page content:
<?php
/**
* Template part for displaying page content.
*/
?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<header class="entry-header container">
<h2 class="entry-title"><?php the_title(); ?></h2>
<?php example_the_page_intro(); ?>
</header>
<div class="entry-content">
<div class="container">
<?php
add_filter( 'render_block', 'example_filter_render_block_intro', 10, 2 );
the_content();
?>
</div>
</div><!-- .entry-content -->
</article><!-- #post-<?php the_ID(); ?> -->
This way, the render_block
filter has already done its work for the intro section block, and consequently strips the intro section block right before rendering the page content.