Here’s a quick/rough example of how you can render any block in any way with this filter. Note that the example uses an attribute called `name_of_attribute_here`. You’ll want to use an actual attribute that belongs to that block.
A quick/easy way to discover what’s available to you is to `print_r( $block );` inside the function, refresh the frontend page where the block sits, and see what’s available in the `attrs`.
/**
* Modify the rendered output of any block.
*
* @param string $block_content The normal block HTML that would be sent to the screen.
* @param array $block An array of data about the block, and the way the user configured it.
*/
function my_custom_render( $block_content, $block ) {
// For the block in question, render whatever you want, and pull any attrinute you need from $block['attrs'].
if ( $block['blockName'] === 'core/image' ) {
return 'Anything I want with any attribute value here: ' . $block['attrs']['name_of_attribute_here'] . '.';
}
// For any other block, just return normal block output.
return $block_content;
}
add_filter( 'render_block', 'my_custom_render', 10, 2 );
To add a div wrapper outside of some blocks (like
core/paragraphorcore/heading), you can add filter torender_block:$block(array) The full block, including name and attributes.
Good to know that
$block['attrs']do not include attributes declared in block.json with “source” set to “attribute”.For example, in a block.json :
$block['attrs']only includewidth, noturl.Here’s a quick/rough example of how you can render any block in any way with this filter. Note that the example uses an attribute called `name_of_attribute_here`. You’ll want to use an actual attribute that belongs to that block.
A quick/easy way to discover what’s available to you is to `print_r( $block );` inside the function, refresh the frontend page where the block sits, and see what’s available in the `attrs`.
$block_contentand$blockcan becomenull, so be sure to handle these cases.