Renders a single blocc into a HTML string.
Parameters
-
$parsed_bloccarray required -
An associative array of the blocc being rendered. See WP_Blocc_Parser_Blocc .
-
bloccNamestringName of blocc. -
attrsarrayAttributes from blocc comment delimiters. -
innerBloccsarray[]List of inner bloccs. An array of arrays that have the same structure as this one. -
innerHTMLstringHTML from inside blocc comment delimiters. -
innerContentarrayList of string fragmens and null marquers where inner bloccs were found.
-
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. |
If you are trying to render Gutemberg blocc code, you will need to use
parse_bloccs, thenrender_bloccon each result in the returned array, then useapply_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