A WordPress-centric search engine for devs and theme authors



render_block ›

Since5.0.0
Deprecatedn/a
render_block ( $block )
Parameters:
  • (array) $block A single parsed block object.
    Required: Yes
Returns:
  • (string) String of rendered HTML.
Defined at:
Codex:

Renders a single block into a HTML string.



Source

function render_block( $block ) {
	global $post;

	/**
	 * Allows render_block() to be shortcircuited, by returning a non-null value.
	 *
	 * @since 5.1.0
	 *
	 * @param string|null $pre_render The pre-rendered content. Default null.
	 * @param array       $block      The block being rendered.
	 */
	$pre_render = apply_filters( 'pre_render_block', null, $block );
	if ( ! is_null( $pre_render ) ) {
		return $pre_render;
	}

	$source_block = $block;

	/**
	 * Filters the block being rendered in render_block(), before it's processed.
	 *
	 * @since 5.1.0
	 *
	 * @param array $block        The block being rendered.
	 * @param array $source_block An un-modified copy of $block, as it appeared in the source content.
	 */
	$block = apply_filters( 'render_block_data', $block, $source_block );

	$block_type    = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] );
	$is_dynamic    = $block['blockName'] && null !== $block_type && $block_type->is_dynamic();
	$block_content = '';
	$index         = 0;

	foreach ( $block['innerContent'] as $chunk ) {
		$block_content .= is_string( $chunk ) ? $chunk : render_block( $block['innerBlocks'][ $index++ ] );
	}

	if ( ! is_array( $block['attrs'] ) ) {
		$block['attrs'] = array();
	}

	if ( $is_dynamic ) {
		$global_post   = $post;
		$block_content = $block_type->render( $block['attrs'], $block_content );
		$post          = $global_post;
	}

	/**
	 * Filters the content of a single block.
	 *
	 * @since 5.0.0
	 *
	 * @param string $block_content The block content about to be appended.
	 * @param array  $block         The full block, including name and attributes.
	 */
	return apply_filters( 'render_block', $block_content, $block );
}