Title: parse_blocks
Published: December 6, 2018
Last modified: February 24, 2026

---

# parse_blocks( string $content ): array[]

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#description)
 * [Parameters](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#source)
 * [Hooks](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#hooks)
 * [Related](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#wp--skip-link--target)

Parses blocks out of a content string.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#description)󠁿

Given an HTML document, this function fully-parses block content, producing a tree
of blocks and their contents, as well as top-level non-block content, which will
appear as a block with no `blockName`.

This function can be memory heavy for certain documents, particularly those with
deeply-nested blocks or blocks with extensive attribute values. Further, this function
must parse an entire document in one atomic operation.

If the entire parsed document is not necessary, consider using [WP_Block_Processor](https://developer.wordpress.org/reference/classes/wp_block_processor/)
instead, as it provides a streaming and low-overhead interface for finding blocks.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#parameters)󠁿

 `$content`stringrequired

Post content.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#return)󠁿

 array[] Array of block structures.

 * `...$0` array
 *  An associative array of a single parsed block object. See [WP_Block_Parser_Block](https://developer.wordpress.org/reference/classes/wp_block_parser_block/).
    - `blockName` string|null
    - Name of block.
    - `attrs` array
    - Attributes from block comment delimiters.
    - `innerBlocks` array[]
    - List of inner blocks. An array of arrays that have the same structure as this
      one.
    - `innerHTML` string
    - HTML from inside block comment delimiters.
    - `innerContent` array
    - List of string fragments and null markers where inner blocks were found.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#source)󠁿

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

[View all references](https://developer.wordpress.org/reference/files/wp-includes/blocks.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/blocks.php#L2419)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/blocks.php#L2419-L2431)

## 󠀁[Hooks](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#hooks)󠁿

 [apply_filters( ‘block_parser_class’, string $parser_class )](https://developer.wordpress.org/reference/hooks/block_parser_class/)

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#related)󠁿

| Uses | Description | 
| [apply_filters()](https://developer.wordpress.org/reference/functions/apply_filters/)`wp-includes/plugin.php` |

Calls the callback functions that have been added to a filter hook.

  |

| Used by | Description | 
| [resolve_pattern_blocks()](https://developer.wordpress.org/reference/functions/resolve_pattern_blocks/)`wp-includes/blocks.php` |

Replaces patterns in a block tree with their content.

  | 
| [apply_block_hooks_to_content()](https://developer.wordpress.org/reference/functions/apply_block_hooks_to_content/)`wp-includes/blocks.php` |

Runs the hooked blocks algorithm on the given content.

  | 
| [update_ignored_hooked_blocks_postmeta()](https://developer.wordpress.org/reference/functions/update_ignored_hooked_blocks_postmeta/)`wp-includes/blocks.php` |

Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.

  | 
| [inject_ignored_hooked_blocks_metadata_attributes()](https://developer.wordpress.org/reference/functions/inject_ignored_hooked_blocks_metadata_attributes/)`wp-includes/block-template-utils.php` |

Inject ignoredHookedBlocks metadata attributes into a template or template part.

  | 
| [wp_get_post_content_block_attributes()](https://developer.wordpress.org/reference/functions/wp_get_post_content_block_attributes/)`wp-includes/block-editor.php` |

Retrieves Post Content block attributes from the current post template.

  | 
| [WP_REST_Block_Patterns_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_block_patterns_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-block-patterns-controller.php` |

Prepare a raw block pattern before it gets output in a REST API response.

  | 
| [wp_generate_block_templates_export_file()](https://developer.wordpress.org/reference/functions/wp_generate_block_templates_export_file/)`wp-includes/block-template-utils.php` |

Creates an export of the current templates and template parts from the site editor at the specified path in a ZIP file.

  | 
| [_inject_theme_attribute_in_block_template_content()](https://developer.wordpress.org/reference/functions/_inject_theme_attribute_in_block_template_content/)`wp-includes/deprecated.php` |

Parses wp_template content and injects the active theme’s stylesheet as a theme attribute into each wp_template_part

  | 
| [_remove_theme_attribute_in_block_template_content()](https://developer.wordpress.org/reference/functions/_remove_theme_attribute_in_block_template_content/)`wp-includes/deprecated.php` |

Parses a block template and removes the theme attribute from each template part.

  | 
| [WP_Widget_Block::get_dynamic_classname()](https://developer.wordpress.org/reference/classes/wp_widget_block/get_dynamic_classname/)`wp-includes/widgets/class-wp-widget-block.php` |

Calculates the classname to use in the block widget’s container HTML.

  | 
| [WP_REST_Templates_Controller::prepare_item_for_response()](https://developer.wordpress.org/reference/classes/wp_rest_templates_controller/prepare_item_for_response/)`wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php` |

Prepare a single template output for response

  | 
| [filter_block_content()](https://developer.wordpress.org/reference/functions/filter_block_content/)`wp-includes/blocks.php` |

Filters and sanitizes block content to remove non-allowable HTML from parsed block attribute values.

  | 
| [do_blocks()](https://developer.wordpress.org/reference/functions/do_blocks/)`wp-includes/blocks.php` |

Parses dynamic blocks out of `post_content` and re-renders them.

  | 
| [excerpt_remove_blocks()](https://developer.wordpress.org/reference/functions/excerpt_remove_blocks/)`wp-includes/blocks.php` |

Parses blocks out of a content string, and renders those appropriate for the excerpt.

  | 
| [get_post_galleries()](https://developer.wordpress.org/reference/functions/get_post_galleries/)`wp-includes/media.php` |

Retrieves galleries from the passed post’s content.

  |

[Show 10 more](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#)

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#changelog)󠁿

| Version | Description | 
| [5.0.0](https://developer.wordpress.org/reference/since/5.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 6 content](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#comment-content-4141)
 2.    [Joseph Dickson](https://profiles.wordpress.org/josephdickson/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-4141)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-4141)
     Vote results for this note: 6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-4141)
 4.  **Example using `parse_blocks()` to display a block from a post**
 5.      ```php
         <?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;
     
         		}
     
         	}
     
         }
     
         ?>
         ```
     
 6.  **Place within [The Loop](https://codex.wordpress.org/The_Loop)**
 7.      ```php
         <?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();
     
         }
     
         ?>
         ```
     
 8.  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.
 9.   * This embed example is now defunct as of WordPress 5.6 changing to the block
        name to core/embed and adding variations. I’ll submit a new sample.
      * [Joseph Dickson](https://profiles.wordpress.org/josephdickson/) [5 years ago](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-4661)
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%3Freplytocom%3D4141%23feedback-editor-4141)
 11.  [Skip to note 7 content](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#comment-content-5389)
 12.   [Ulrich](https://profiles.wordpress.org/grapplerulrich/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-5389)
 13. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-5389)
     Vote results for this note: 5[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-5389)
 14. Use [serialize_blocks()](https://developer.wordpress.org/reference/functions/serialize_blocks/)
     to convert the parsed block back to content.
 15.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%3Freplytocom%3D5389%23feedback-editor-5389)
 16.  [Skip to note 8 content](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#comment-content-3382)
 17.   [Jb Audras](https://profiles.wordpress.org/audrasjb/)  [  7 years ago  ](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-3382)
 18. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-3382)
     Vote results for this note: 2[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-3382)
 19. Example of returned array for a Paragraph block:
 20.     ```php
         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!',
         		), 
         	),
         )
         ```
     
 21.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%3Freplytocom%3D3382%23feedback-editor-3382)
 22.  [Skip to note 9 content](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#comment-content-3702)
 23.   [André Maneiro](https://profiles.wordpress.org/oandregal/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-3702)
 24. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-3702)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-3702)
 25. As of Gutenberg 7.7, for this input:
 26.     ```javascript
         <!-- 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 -->
         ```
     
 27. the output of `parse_blocks` will be:
 28.     ```php
         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>
                 )
               )
         )
         ```
     
 29.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%3Freplytocom%3D3702%23feedback-editor-3702)
 30.  [Skip to note 10 content](https://developer.wordpress.org/reference/functions/parse_blocks/?output_format=md#comment-content-5123)
 31.   [Khoi Pro](https://profiles.wordpress.org/khoipro/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-5123)
 32. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-5123)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%23comment-5123)
 33. In case you wish to check if current post content is Gutenberg-blocks or using
     Classic editor:
 34.     ```php
             global $post;
             $blocks = parse_blocks( $post->post_content );
             $is_gutenberg_page = ( ! empty( $blocks ) && '' !== $blocks[0]['blockName'] );
         ```
     
 35. If post content is Classic Editor, it has only one block, but `blockName` is empty.
 36.  * There is a better way for this. Function [has_blocks()](https://developer.wordpress.org/reference/functions/has_blocks/).
      * [Lovro Hrust](https://profiles.wordpress.org/lovor/) [4 years ago](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-6112)
      * Currently, a much less costly method would use the [has_blocks()](https://developer.wordpress.org/reference/functions/has_blocks/)
        function, which accepts either the unparsed post content or a wp_post object.`
        $fullContent = get_the_content( $pid ); if ( has_blocks( $fullContent ) ) {
        doit(); } ` or ` if ( has_blocks( $post ) ) { doit(); } `
      * [Brian Layman](https://profiles.wordpress.org/brianlayman/) [3 years ago](https://developer.wordpress.org/reference/functions/parse_blocks/#comment-6242)
 37.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F%3Freplytocom%3D5123%23feedback-editor-5123)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fparse_blocks%2F)
before being able to contribute a note or feedback.