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 Andrés

    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.