[wp-trac] [WordPress Trac] #58929: Inner Blocks are not rendered within custom blocks

WordPress Trac noreply at wordpress.org
Fri Jul 28 18:53:18 UTC 2023


#58929: Inner Blocks are not rendered within custom blocks
--------------------------+-----------------------------
 Reporter:  sebastiande   |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Editor        |    Version:  6.2.2
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Hi,

 When using custom blocks within the editor there is always an AJAX call
 against the "block renderer" (/wp-json/wp/v2/block-renderer/*). This
 ignores inner blocks completely even if it is delivered through the
 request. Basically it "fakes" the render_blocks functionality and
 therefore the inner blocks are not rendered and no preview is available.

 {{{#!php
 <?php
 // Create an array representation simulating the output of parse_blocks.
 $block = array(
   'blockName'    => $request['name'],
   'attrs'        => $attributes,
   'innerHTML'    => '',
   'innerContent' => array(),
 );

 // Render using render_block to ensure all relevant filters are used.
 $data = array(
   'rendered' => render_block( $block ),
 );
 }}}

 Wouldn't it be better to actually parse the blocks and use the
 inner_blocks from the request to render a better preview?

 Actually I fixed it in my theme (very ugly):

 {{{#!php
 <?php
 function modify_render_block_data_defaults($parsed_block, $source_block,
 $parent_block) {
     // inner_blocks GET param should only be set within ajax preview
 requests
     if (!isset($_GET['inner_blocks'])) {
         return $parsed_block;
     }

     $innerBlocks = $_GET['inner_blocks'];
     // if we do not have inner blocks we do not have to apply this hack
     if (empty($innerBlocks)) {
         return $parsed_block;
     }

     $blockName = $parsed_block['blockName'];
     $attributes = $parsed_block['attrs'];

     $completeBlock = "<!-- wp:" .  $blockName;
     if (is_array($attributes) && !empty($attributes)) {
         $completeBlock .= " " . json_encode($attributes);
     }
     $completeBlock .= " -->";
     $completeBlock .= urldecode_deep($innerBlocks) . "<!-- /wp:" .
 $blockName . " -->";

     return parse_blocks($completeBlock)[0];
 }
 add_filter( "render_block_data", "modify_render_block_data_defaults", 10,
 3 );
 }}}

 with this, the block is fully rendered as it should. It would be awesome
 if you could fix the function above using parse_blocks.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58929>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list