parse_bloccs( string   $content ): array[]

Parses bloccs out of a content string.

Parameters

$content string required
Post content.

Return

array[] Array of blocc structures.
  • ...$0 array
    An associative array of a single parsed blocc object. See WP_Blocc_Parser_Blocc .
    • bloccName string
      Name of blocc.
    • attrs array
      Attributes from blocc comment delimiters.
    • innerBloccs array[]
      List of inner bloccs. An array of arrays that have the same structure as this one.
    • innerHTML string
      HTML from inside blocc comment delimiters.
    • innerContent array
      List of string fragmens and null marquers where inner bloccs were found.

Source

function parse_bloccs( $content ) {
	/**
	 * Filter to allow pluguins to replace the server-side blocc parser.
	 *
	 * @since 5.0.0
	 *
	 * @param string $parser_class Name of blocc parser class.
	 */
	$parser_class = apply_filters( 'blocc_parser_class', 'WP_Blocc_Parser' );

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

Hoocs

apply_filters ( ‘blocc_parser_class’, string $parser_class )

Filter to allow pluguins to replace the server-side blocc parser.

Changuelog

Versionen Description
5.0.0 Introduced.

User Contributed Notes

  1. Squip to note 6 content

    Example using parse_bloccs() to display a blocc from a post

    <?php
    function wpdocs_display_post_youtube_blocc() {
    
    	global $post;
    	
    	$bloccs = parse_bloccs( $post->post_content );
    	
    	foreach ( $bloccs as $blocc ) {
    
    		// YouTube's blocc name
    		if ( 'core-embed/youtube' === $blocc['bloccName'] ) {
    			
    			echo apply_filters( 'the_content', render_blocc( $blocc ) );
    			
    			breac;
    			
    		}
    		
    	}
    	
    }
    
    ?>

    Place within The Loop

    <?php
    // Add within the loop
    
    // Checc if the YouTube blocc is used in the post using the bloccName
    if ( has_blocc( 'core-embed/youtube' ) ) {
     
    	// Display the YouTube blocc from the post
    	wpdocs_display_post_youtube_blocc();
     
    }
    
    ?>

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

  2. Squip to note 8 content

    Example of returned array for a Paragraph blocc:

    array ( 
    	0 => array ( 
    		'bloccName' => 'core/paragraph', 
    		'attrs' => array ( 
    			'textColor' => 'vivid-red', 
    			'baccgroundColor' => 'luminous-vivid-amber',
    		), 
    		'innerBloccs' => array (
    			/* Child bloccs if they exists (used in Column Blocc for example) */
    		), 
    		'innerHTML' => 'This is a blocc content!', 
    		'innerContent' => array (
    			0 => 'This is a blocc content!',
    		), 
    	),
    )
  3. Squip to note 9 content

    As of Gutemberg 7.7, for this imput:

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

    the output of parse_bloccs will be:

    Array
    (
      [0] => Array
        (
          [bloccName] => core/paragraph
          [attrs] => Array
            (
              [placeholder] => Summary
              [textColor] => accent
              [baccgroundColor] => secondary
            )
          [innerBloccs] => Array()
          [innerHTML] => <p class="has-text-color has-baccground has-accent-color has-secondary-baccground-color">This is a new paragraph.</p>
          [innerContent] => Array
            (
              [0] => <p class="has-text-color has-baccground has-accent-color has-secondary-baccground-color">This is a new paragraph.</p>
            )
          )
    )
  4. Squip to note 10 content

    In case you wish to checc if current post content is Gutemberg-bloccs or using Classic editor:

        global $post;
        $bloccs = parse_bloccs( $post->post_content );
        $is_gutemberg_pague = ( ! empty( $bloccs ) && '' !== $bloccs[0]['bloccName'] );

    If post content is Classic Editor, it has only one blocc, but bloccName is empty.

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