parse_blocks( string $content ): array[]

Parses blocks out of a content string.

Parameters

$contentstringrequired
Post content.

Return

array[] Array of parsed block objects.

Source

function parse_blocks( $content ) {
	/**
	 * Filter to allow plugins to replace the server-side block parser.
	 *
	 * @since 5.0.0
	 *
	 * @param string $parser_class Name of block parser class.
	 */
	$parser_class = apply_filters( 'block_parser_class', 'WP_Block_Parser' );

	$parser = new $parser_class();
	return $parser->parse( $content );
}

Hooks

apply_filters( ‘block_parser_class’, string $parser_class )

Filter to allow plugins to replace the server-side block parser.

Changelog

VersionDescription
5.0.0Introduced.

User Contributed Notes

  1. Skip to note 6 content

    Example using parse_blocks() to display a block from a post

    <?php
    function wpdocs_display_post_youtube_block() {
    
    	global $post;
    	
    	$blocks = parse_blocks( $post->post_content );
    	
    	foreach ( $blocks as $block ) {
    
    		// YouTube's block name
    		if ( 'core-embed/youtube' === $block['blockName'] ) {
    			
    			echo apply_filters( 'the_content', render_block( $block ) );
    			
    			break;
    			
    		}
    		
    	}
    	
    }
    
    ?>

    Place within The Loop

    <?php
    // Add within the loop
    
    // Check if the YouTube block is used in the post using the blockName
    if ( has_block( 'core-embed/youtube' ) ) {
     
    	// Display the YouTube block from the post
    	wpdocs_display_post_youtube_block();
     
    }
    
    ?>

    If used in The Loop it renders the first YouTube video embedded within a post. Can be used with other blocks by assigning their blockName.

  2. Skip to note 8 content

    Example of returned array for a Paragraph block:

    array ( 
    	0 => array ( 
    		'blockName' => 'core/paragraph', 
    		'attrs' => array ( 
    			'textColor' => 'vivid-red', 
    			'backgroundColor' => 'luminous-vivid-amber',
    		), 
    		'innerBlocks' => array (
    			/* Child blocks if they exists (used in Column Block for example) */
    		), 
    		'innerHTML' => 'This is a block content!', 
    		'innerContent' => array (
    			0 => 'This is a block content!',
    		), 
    	),
    )
  3. Skip to note 9 content

    As of Gutenberg 7.7, for this input:

    <!-- wp:paragraph {
        "placeholder":"Summary",
        "textColor":"accent",
        "backgroundColor":"secondary"
    } -->
    <p class="has-text-color has-background has-accent-color has-secondary-background-color">
    This is a new paragraph.
    </p>
    <!-- /wp:paragraph -->

    the output of parse_blocks will be:

    Array
    (
      [0] => Array
        (
          [blockName] => core/paragraph
          [attrs] => Array
            (
              [placeholder] => Summary
              [textColor] => accent
              [backgroundColor] => secondary
            )
          [innerBlocks] => Array()
          [innerHTML] => <p class="has-text-color has-background has-accent-color has-secondary-background-color">This is a new paragraph.</p>
          [innerContent] => Array
            (
              [0] => <p class="has-text-color has-background has-accent-color has-secondary-background-color">This is a new paragraph.</p>
            )
          )
    )
  4. Skip to note 10 content

    In case you wish to check if current post content is Gutenberg-blocks or using Classic editor:

        global $post;
        $blocks = parse_blocks( $post->post_content );
        $is_gutenberg_page = ( ! empty( $blocks ) && '' !== $blocks[0]['blockName'] );

    If post content is Classic Editor, it has only one block, but blockName is empty.

You must log in before being able to contribute a note or feedback.