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

gutenberg_render_block does not render inner blocks #8214

Closed
brucepearson opened this issue Jul 26, 2018 · 4 comments
Closed

gutenberg_render_block does not render inner blocks #8214

brucepearson opened this issue Jul 26, 2018 · 4 comments
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P Needs Testing Needs further testing to be confirmed.

Comments

@brucepearson
Copy link

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

@designsimply designsimply added Needs Testing Needs further testing to be confirmed. [Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P labels Jul 26, 2018
brucepearson pushed a commit to OnTheGoSystems/gutenberg that referenced this issue Aug 9, 2018
@aduth
Copy link
Member

aduth commented Feb 4, 2019

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.

@aduth aduth closed this as completed Feb 4, 2019
@ryanapsmith
Copy link

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.

@aduth
Copy link
Member

aduth commented May 21, 2019

@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

@ryanapsmith
Copy link

@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
Labels
[Feature] Nested / Inner Blocks Anything related to the experience of nested/inner blocks inside a larger container, like Group or P Needs Testing Needs further testing to be confirmed.
Projects
None yet
Development

No branches or pull requests

4 participants