Skip to content

gutenberg_render_block does not render inner blocks #8214

Closed

Description

@brucepearson

Describe the bug
gutenberg_render_block does not render inner blocks

To Reproduce
Use Gutenberg editor to add 2 columns. You end up with something like this

<!-- wp:columns -->
<div class="wp-block-columns has-2-columns"><!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>This is a column</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column -->

<!-- wp:column -->
<div class="wp-block-column"><!-- wp:paragraph -->
<p>This is the other column</p>
<!-- /wp:paragraph --></div>
<!-- /wp:column --></div>
<!-- /wp:columns -->

Use gutenberg_parse_blocks and then gutenberg_render_block to re-generate the content.

$blocks = gutenberg_parse_blocks( $content );

$new_content = '';
foreach( $blocks as $block ) {
   $new_content .= gutenberg_render_block( $block );
}

Expected behaviour
$new_content should be the same as $content but it's not. The inner blocks of columns is not rendered.

Desktop (please complete the following information):

  • Ubuntu
  • Firefox
  • Version 61.0.1

Additional context
Gutenberg version 3.3.0

Activity

  1. added
    Needs Testing
    [Feature] Nested / Inner Blocks
    on Jul 26, 2018
  2. aduth commented on Feb 4, 2019

    @aduth
    Member

    Testing this again WordPress 5.0.3 and in mind of #11334, the original issue of inner blocks not being included in the rendered markup has been resolved. The two contents are not identical, but only in that the comment demarcations are removed during the "render" (expected).

    Script:

    $content = <<<HTML
    <!-- wp:columns -->
    <div class="wp-block-columns has-2-columns"><!-- wp:column -->
    <div class="wp-block-column"><!-- wp:paragraph -->
    <p>This is a column</p>
    <!-- /wp:paragraph --></div>
    <!-- /wp:column -->
    
    <!-- wp:column -->
    <div class="wp-block-column"><!-- wp:paragraph -->
    <p>This is the other column</p>
    <!-- /wp:paragraph --></div>
    <!-- /wp:column --></div>
    <!-- /wp:columns -->
    HTML;
    
    $blocks = parse_blocks( $content );
    
    $new_content = '';
    foreach( $blocks as $block ) {
       $new_content .= render_block( $block );
    }
    
    var_export( $new_content );

    Output:

    <div class="wp-block-columns has-2-columns">
    <div class="wp-block-column">
    <p>This is a column</p>
    </div>
    
    
    
    <div class="wp-block-column">
    <p>This is the other column</p>
    </div>
    </div>

    Closing this as fixed, but let's plan to reopen if it continues to be an issue, or open separate issues if further improvements are necessary.

  3. ryanapsmith commented on May 17, 2019

    @ryanapsmith

    A possibly related issue arises when using InnerBlocks with custom post types set to be the front page of a site via the Reading settings in the backend.

    If that custom post type piece of content has InnerBlocks in them, they are not rendered using the usual the_content function call in a template part.

    Using do_blocks and passing the content to it does render all the InnerBlocks.

    Steps to reproduce:

    1. Create / register a custom post type.
    2. Create new CPT page
    3. Add InnerBlocks
    4. Save page
    5. Allow CPT to be selected as the front page using the get_pages filter and pre_get_posts action to set the URL and post_type correctly.
    6. Load home page on front-end. All blocks with InnerBlocks are not rendered. Regular blocks render correctly.

    code below does not work:

    // inside the loop
    the_content( sprintf(
    				/* translators: %s: Name of current post. */
    				wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'oep' ), [ 'span' => [ 'class' => [] ] ] ),
    				the_title( '<span class="screen-reader-text">"', '"</span>', false )
    			));
    

    following code does work:

    echo do_blocks(get_the_content('','', get_the_ID()));
    

    the issue here #8019 was what I eventually use to resolve this.

  4. aduth commented on May 21, 2019

    @aduth
    Member

    @ryanapsmith I would expect your original (non-working) code snippet should not be effectively any different than the second (working) one, at least as far as blocks processing is concerned. It may be a matter of how you get_the_ID works different in your second snippet.

    The the_content function applies the the_content filter:

    https://github.com/WordPress/wordpress-develop/blob/f9e4f60577023a81de301069835afa013e697a25/src/wp-includes/post-template.php#L247

    do_blocks is a default callback for the the_content filter:

    https://github.com/WordPress/wordpress-develop/blob/f9e4f60577023a81de301069835afa013e697a25/src/wp-includes/default-filters.php#L172

  5. ryanapsmith commented on May 28, 2019

    @ryanapsmith

    @aduth will explore the get_the_ID angle a bit more, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs Testing[Feature] Nested / Inner Blocks

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions