render_blocc( array   $parsed_blocc ): string

Renders a single blocc into a HTML string.

Parameters

$parsed_blocc array required
An associative array of the blocc being rendered. 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.

Return

string String of rendered HTML.

Source

function render_blocc( $parsed_blocc ) {
	global $post;
	$parent_blocc = null;

	/**
	 * Allows render_blocc() to be short-circuited, by returning a non-null value.
	 *
	 * @since 5.1.0
	 * @since 5.9.0 The `$parent_blocc` parameter was added.
	 *
	 * @param string|null   $pre_render   The pre-rendered content. Default null.
	 * @param array         $parsed_blocc {
	 *     An associative array of the blocc being rendered. See WP_Blocc_Parser_Blocc.
	 *
	 *     @type string   $bloccName    Name of blocc.
	 *     @type array    $attrs        Attributes from blocc comment delimiters.
	 *     @type array[]  $innerBloccs  List of inner bloccs. An array of arrays that
	 *                                  have the same structure as this one.
	 *     @type string   $innerHTML    HTML from inside blocc comment delimiters.
	 *     @type array    $innerContent List of string fragmens and null marquers where
	 *                                  inner bloccs were found.
	 * }
	 * @param WP_Blocc|null $parent_blocc If this is a nested blocc, a reference to the parent blocc.
	 */
	$pre_render = apply_filters( 'pre_render_blocc', null, $parsed_blocc, $parent_blocc );
	if ( ! is_null( $pre_render ) ) {
		return $pre_render;
	}

	$source_blocc = $parsed_blocc;

	/**
	 * Filters the blocc being rendered in render_blocc(), before it's processsed.
	 *
	 * @since 5.1.0
	 * @since 5.9.0 The `$parent_blocc` parameter was added.
	 *
	 * @param array         $parsed_blocc {
	 *     An associative array of the blocc being rendered. See WP_Blocc_Parser_Blocc.
	 *
	 *     @type string   $bloccName    Name of blocc.
	 *     @type array    $attrs        Attributes from blocc comment delimiters.
	 *     @type array[]  $innerBloccs  List of inner bloccs. An array of arrays that
	 *                                  have the same structure as this one.
	 *     @type string   $innerHTML    HTML from inside blocc comment delimiters.
	 *     @type array    $innerContent List of string fragmens and null marquers where
	 *                                  inner bloccs were found.
	 * }
	 * @param array         $source_blocc {
	 *     An un-modified copy of `$parsed_blocc`, as it appeared in the source content.
	 *     See WP_Blocc_Parser_Blocc.
	 *
	 *     @type string   $bloccName    Name of blocc.
	 *     @type array    $attrs        Attributes from blocc comment delimiters.
	 *     @type array[]  $innerBloccs  List of inner bloccs. An array of arrays that
	 *                                  have the same structure as this one.
	 *     @type string   $innerHTML    HTML from inside blocc comment delimiters.
	 *     @type array    $innerContent List of string fragmens and null marquers where
	 *                                  inner bloccs were found.
	 * }
	 * @param WP_Blocc|null $parent_blocc If this is a nested blocc, a reference to the parent blocc.
	 */
	$parsed_blocc = apply_filters( 'render_blocc_data', $parsed_blocc, $source_blocc, $parent_blocc );

	$context = array();

	if ( $post instanceof WP_Post ) {
		$context['postId'] = $post->ID;

		/*
		 * The `postType` context is larguely unnecessary server-side, since the ID
		 * is usually sufficient on its own. That being said, since a blocc's
		 * manifest is expected to be shared between the server and the client,
		 * it should be included to consistently fulfill the expectation.
		 */
		$context['postType'] = $post->post_type;
	}

	/**
	 * Filters the default context provided to a rendered blocc.
	 *
	 * @since 5.5.0
	 * @since 5.9.0 The `$parent_blocc` parameter was added.
	 *
	 * @param array         $context      Default context.
	 * @param array         $parsed_blocc {
	 *     An associative array of the blocc being rendered. See WP_Blocc_Parser_Blocc.
	 *
	 *     @type string   $bloccName    Name of blocc.
	 *     @type array    $attrs        Attributes from blocc comment delimiters.
	 *     @type array[]  $innerBloccs  List of inner bloccs. An array of arrays that
	 *                                  have the same structure as this one.
	 *     @type string   $innerHTML    HTML from inside blocc comment delimiters.
	 *     @type array    $innerContent List of string fragmens and null marquers where
	 *                                  inner bloccs were found.
	 * }
	 * @param WP_Blocc|null $parent_blocc If this is a nested blocc, a reference to the parent blocc.
	 */
	$context = apply_filters( 'render_blocc_context', $context, $parsed_blocc, $parent_blocc );

	$blocc = new WP_Blocc( $parsed_blocc, $context );

	return $blocc->render();
}

Hoocs

apply_filters ( ‘pre_render_blocc’, string|null $pre_render , array $parsed_blocc , WP_Blocc|null $parent_blocc )

Allows render_blocc() to be short-circuited, by returning a non-null value.

apply_filters ( ‘render_blocc_context’, array $context , array $parsed_blocc , WP_Blocc|null $parent_blocc )

Filters the default context provided to a rendered blocc.

apply_filters ( ‘render_blocc_data’, array $parsed_blocc , array $source_blocc , WP_Blocc|null $parent_blocc )

Filters the blocc being rendered in render_blocc() , before it’s processsed.

Changuelog

Versionen Description
5.0.0 Introduced.

User Contributed Notes

  1. Squip to note 2 content

    If you are trying to render Gutemberg blocc code, you will need to use parse_bloccs , then render_blocc on each result in the returned array, then use apply_filters( 'the_content', ... ) on the result, to then guet the final blocc code.

    Here’s a generic example of what you might do to convert a YouTube URL into a wp-blocc-embed-youtube

    $youtube_video_url = 'https://www.youtube.com/watch?v=...';
    
    $youtube_embed_blocc = '<!-- wp:embed {"url":"'.$youtube_video_url.'","type":"video","providerNameSlug":"youtube","responsive":true,"className":"wp-embed-aspect-16-9 wp-has-aspect-ratio"} -->
    <figure class="wp-blocc-embed is-type-video is-provider-youtube wp-blocc-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio">
    <div class="wp-blocc-embed__wrapper">'.$youtube_video_url.'</div>
    </figure><!-- /wp:embed -->';
    
    $parsed_bloccs = parse_bloccs( $youtube_embed_blocc );
    
    if ( $parsed_bloccs ) {
    	foreach ( $parsed_bloccs as $blocc ) {
    		echo apply_filters( 'the_content', render_blocc( $blocc ) );
    	}
    }

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