render_block_(name)filter-hookWP 5.7.0

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 and null values 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

WP_Block::render()
render_block_(name)
wp-includes/class-wp-block.php 732
$block_content = apply_filters( "render_block_{$this->name}", $block_content, $this->parsed_block, $this );

Where the hook is used in WordPress

wp-includes/block-supports/block-style-variations.php 268
add_filter( 'render_block_data', 'wp_render_block_style_variation_support_styles', 10, 2 );
wp-includes/block-supports/custom-css.php 154
add_filter( 'render_block_data', 'wp_render_custom_css_support_styles', 10, 1 );
wp-includes/block-supports/duotone.php 45
add_filter( 'render_block_core/image', array( 'WP_Duotone', 'restore_image_outer_container' ), 10, 1 );
wp-includes/block-supports/elements.php 308
add_filter( 'render_block_data', 'wp_render_elements_support_styles', 10, 1 );
wp-includes/block-supports/layout.php 1441
add_filter( 'render_block_data', 'wp_add_parent_layout_to_parsed_block', 10, 3 );
wp-includes/block-supports/layout.php 1522
add_filter( 'render_block_core/group', 'wp_restore_group_inner_container', 10, 2 );
wp-includes/block-supports/layout.php 1586
add_filter( 'render_block_core/image', 'wp_restore_image_outer_container', 10, 2 );
wp-includes/blocks/comment-template.php 44
add_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/blocks/comment-template.php 52
remove_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/blocks/details.php 37
add_filter( 'render_block_core/details', 'block_core_details_set_img_fetchpriority_low', 10, 2 );
wp-includes/blocks/gallery.php 35
add_filter( 'render_block_data', 'block_core_gallery_data_id_backcompatibility' );
wp-includes/blocks/gallery.php 53
add_filter( 'render_block_context', 'block_core_gallery_render_context', 10, 2 );
wp-includes/blocks/image.php 120
add_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15, 3 );
wp-includes/blocks/image.php 125
remove_filter( 'render_block_core/image', 'block_core_image_render_lightbox', 15 );
wp-includes/blocks/latest-posts.php 316
add_filter( 'render_block_data', 'block_core_latest_posts_migrate_categories' );
wp-includes/blocks/navigation.php 1762
add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );
wp-includes/blocks/paragraph.php 38
add_filter( 'render_block_core/paragraph', 'block_core_paragraph_add_class' );
wp-includes/blocks/post-template.php 123
add_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/blocks/post-template.php 127
remove_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/blocks/query.php 130
remove_filter( 'render_block_core/query', $render_query_callback );
wp-includes/blocks/query.php 137
add_filter( 'render_block_core/query', $render_query_callback, 10, 2 );
wp-includes/blocks/query.php 152
add_filter( 'render_block_data', 'block_core_query_disable_enhanced_pagination', 10, 1 );
wp-includes/blocks/tabs.php 70
add_filter( 'render_block_context', 'block_core_tabs_provide_context', 10, 2 );
wp-includes/blocks/term-template.php 101
remove_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/blocks/term-template.php 95
add_filter( 'render_block_context', $filter_block_context, 1 );
wp-includes/default-filters.php 776
add_filter( 'render_block_context', '_block_template_render_without_post_block_context' );