render_block_(name)
Allows changing the HTML of the specified block after it is rendered.
Replace (name) with the full block name including its namespace:
render_block_core/paragraph— paragraph.render_block_core/image— image.render_block_core/group— group.render_block_my-plugin/example— custom block.
The filter is called in WP_Block::render() after the general render_block filter. It works for static and dynamic blocks, including nested ones. It changes the output but not the saved post content.
If block rendering is interrupted through pre_render_block, this filter is not called for it.
To process all block types, use render_block. To replace the result before rendering, use pre_render_block.
Usage
add_filter( 'render_block_(name)', 'wp_kama_render_block_name_filter', 10, 3 );
/**
* Function for `render_block_(name)` filter-hook.
*
* @param string $block_content The block content.
* @param array $block The full block, including name and attributes.
* @param WP_Block $instance The block instance.
*
* @return string
*/
function wp_kama_render_block_name_filter( $block_content, $block, $instance ){
// filter...
return $block_content;
}
- $block_content(string)
Block HTML after rendering and processing by previous filters.
Return the modified HTML or the original value. An empty string
''removes the block from the output, but rendering itself has already been performed at this point.- $block(array)
Block data.
-
blockName(string)
Full block name, for example,core/image. -
attrs(array)
Block attributes from its HTML comment. Attributes extracted from HTML can be absent. -
innerBlocks(array of arrays)
Nested blocks with the same data structure. -
innerHTML(string)
Original block HTML without the HTML of nested blocks. It is not equivalent to the finished$block_content. - innerContent(array)
HTML fragments andnullvalues at the positions of nested blocks.
-
- $instance(WP_Block)
- Current WP_Block{} object.
Examples
#1 Add a CSS class to all Paragraph blocks
Use WP_HTML_Tag_Processor{} to add a class to the <p> tag while preserving the other attributes.
add_filter( 'render_block_core/paragraph', 'my_add_paragraph_class' );
function my_add_paragraph_class( $block_content ) {
$processor = new WP_HTML_Tag_Processor( $block_content );
if ( $processor->next_tag( 'P' ) ) {
$processor->add_class( 'custom-paragraph' );
}
return $processor->get_updated_html();
}
#2 Add a wrapper to images of a specific size
Check the sizeSlug attribute and wrap images with the large size in an additional container.
add_filter( 'render_block_core/image', 'my_wrap_large_image', 20, 2 );
function my_wrap_large_image( $block_content, $block ) {
if ( '' === trim( $block_content ) ) {
return $block_content;
}
if ( 'large' !== ( $block['attrs']['sizeSlug'] ?? '' ) ) {
return $block_content;
}
return '<div class="large-image-wrapper">' . $block_content . '</div>';
}
There is no need to check $block['blockName'] here: the callback is called only for core/image.
Changelog
| Since 5.7.0 | Introduced. |
| Since 5.9.0 | The $instance parameter was added. |
Where the hook is called
$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );
Where the hook is used in WordPress
add_filter( 'render_block_data', 'wp_render_block_style_variation_support_styles', 10, 2 );
add_filter( 'render_block_data', 'wp_render_custom_css_support_styles', 10, 1 );
add_filter( 'render_block_core/image', array( 'WP_Duotone', 'restore_image_outer_container' ), 10, 1 );
add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 );
add_filter( 'render_block_data', 'wp_add_parent_layout_to_parsed_block', 10, 3 );
add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );
add_filter( 'render_block_context', $filter_block_context, 1 );
remove_filter( 'render_block_context', $filter_block_context, 1 );
add_filter( 'render_block_core/details', 'block_core_details_set_img_fetchpriority_low', 10, 2 );
add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );
add_filter( 'render_block_context', 'block_core_gallery_render_context', 10, 2 );
add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 3 );
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );
add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );
add_filter( 'render_block_core/paragraph', 'block_core_paragraph_add_class' );
add_filter( 'render_block_context', $filter_block_context, 1 );
remove_filter( 'render_block_context', $filter_block_context, 1 );
remove_filter( 'render_block_core/query', $render_query_callback );
add_filter( 'render_block_core/query', $render_query_callback, 10, 2 );
add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 );
add_filter( 'render_block_context', 'block_core_tabs_provide_context', 10, 2 );
remove_filter( 'render_block_context', $filter_block_context, 1 );
add_filter( 'render_block_context', $filter_block_context, 1 );
add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );