apply_filters( ‘render_block_context’, array $context, array $parsed_block, WP_Block|null $parent_block )

Filters the default context provided to a rendered block.

Parameters

$contextarray
Default context.
$parsed_blockarray
An associative array of the block being rendered. See WP_Block_Parser_Block.
  • blockName string|null
    Name of block.
  • attrs array
    Attributes from block comment delimiters.
  • innerBlocks array[]
    List of inner blocks. An array of arrays that have the same structure as this one.
  • innerHTML string
    HTML from inside block comment delimiters.
  • innerContent array
    List of string fragments and null markers where inner blocks were found.
$parent_blockWP_Block|null
If this is a nested block, a reference to the parent block.

Source

$context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block );

Changelog

VersionDescription
5.9.0The $parent_block parameter was added.
5.5.0Introduced.

User Contributed Notes

  1. Skip to note 2 content

    You can conditionally and also dynamically define context for a particular block:

    /**
     * Modify the block context.
     *
     * @param array $context The block context.
     * @param array $block The block being rendered.
     * @return array
     */
    add_filter( 'render_block_context', function ( $context, $block ) {
    	if ( 'core/paragraph' === $block['blockName'] ) {
    		$context['my-plugin/isExample'] = true;
    	}
    
    	return $context;
    }, 10, 2 );

You must log in before being able to contribute a note or feedback.