parse_blocks( string $content )

Parses blocks out of a content string.


Parameters Parameters

$content

(string) (Required) Post content.


Top ↑

Return Return

(array[]) Array of parsed block objects.


Top ↑

Source Source

File: wp-includes/blocks.php

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 );
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.0.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Jb Audras

    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!',
    		), 
    	),
    )
    
  2. Skip to note 2 content
    Contributed by Joseph Dickson

    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.

  3. Skip to note 3 content
    Contributed by O André

    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>
            )
          )
    )

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