render_block_context
云策文档标注
概述
render_block_context 是一个 WordPress 过滤器钩子,用于修改渲染块时提供的默认上下文。它允许开发者在块渲染过程中动态调整上下文数据。
关键要点
- 过滤器名称:render_block_context
- 主要参数:$context(默认上下文数组)、$parsed_block(被渲染块的关联数组)、$parent_block(父块引用,可为 null)
- 用途:在块渲染时过滤或扩展上下文,常用于自定义块逻辑或数据传递
- 相关函数:与 WP_Block::render() 和 render_block() 紧密关联,影响块输出生成
- 版本历史:WordPress 5.5.0 引入,5.9.0 添加了 $parent_block 参数
代码示例
$context = apply_filters( 'render_block_context', $context, $parsed_block, $parent_block );注意事项
- $parsed_block 参数包含 blockName、attrs、innerBlocks 等子键,结构参考 WP_Block_Parser_Block
- $parent_block 参数在嵌套块中提供父块引用,否则为 null,自 5.9.0 起可用
- 使用此钩子时需确保上下文修改不影响其他块或系统稳定性
原文内容
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.
blockNamestring|nullName of block.attrsarrayAttributes from block comment delimiters.innerBlocksarray[]List of inner blocks. An array of arrays that have the same structure as this one.innerHTMLstringHTML from inside block comment delimiters.innerContentarrayList 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 );