[wp-trac] [WordPress Trac] #45312: parse_blocks return non-existing empty blocks

WordPress Trac noreply at wordpress.org
Thu Apr 18 18:39:11 UTC 2019

#45312: parse_blocks return non-existing empty blocks
 Reporter:  Chouby        |       Owner:  (none)
     Type:  defect (bug)  |      Status:  reopened
 Priority:  normal        |   Milestone:
Component:  Editor        |     Version:  5.0
 Severity:  normal        |  Resolution:
 Keywords:                |     Focuses:

Comment (by dmsnell):

 > re-opened…investigated and fixed

 The reason this was closed is because the behavior was surprising but not
 //wrong//. Specifically the parser is doing everything it should do and
 the source of the unexpected behavior comes from the way that the block
 editor prettifies the output when it saves. It prettifies output by adding
 empty freeform HTML content between blocks in order to make the
 `post_content` more readable.

 To that end I don't see any warrant to reopen this issue as there's
 nothing to fix //here//. We can raise conversation about the editor's
 behavior but I think that needs to happen in a place where the discussion
 is focussed on the behavior in question.


 > I'm adding alternating classes to each, based on the array index being

 I'd be curious to see what code you are using to do this. From how I am
 interpreting what you are describing I'm surprised you are getting errors.
 Would you share a small snippet demonstrating the error?

 $blocks = parse_blocks( $post->post_content );
 foreach ( $blocks as $index => $block ) {
         # ???

 On an unrelated note I would be interested in seeing what specifically you
 are trying to accomplish through adding CSS classes. There may be other
 issues in the way even if these empty blocks weren't there (I did provide
 an example function in the GitHub PR as a way to filter out these blocks).

 You may find
 /wp-includes/blocks.php#L230 block filtering] useful if you are wanting to
 modify the output on page render.

 add_filter( 'render_block', function( $content, $block ) {
         static $is_even = true;

         if ( ! do_I_want_to_modify_this_block( $block ) ) {
                 return $content;

         $is_even = ! $is_even;
         return sprint(
                 '<div class="%s">%s</div>',
                 $is_even ? 'is-even' : 'is-odd',
 } );

Ticket URL: <https://core.trac.wordpress.org/ticket/45312#comment:8>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform

More information about the wp-trac mailing list