钩子文档

hooked_block_types

💡 云策文档标注

概述

hooked_block_types 过滤器用于修改指定锚点块类型和相对位置下的钩入块类型列表。开发者可以通过此过滤器动态添加或移除钩入块,实现块布局的自定义控制。

关键要点

  • 过滤器名称:hooked_block_types,用于过滤钩入块类型列表
  • 参数:$hooked_block_types(钩入块类型数组)、$relative_position(相对位置,可选 'before'、'after'、'first_child'、'last_child')、$anchor_block_type(锚点块类型)、$context(上下文对象,如 WP_Block_Template 或 WP_Post)
  • 用途:在块编辑器中,根据锚点块和位置条件,动态插入自定义块或修改现有钩入块
  • 版本:自 WordPress 6.4.0 引入

代码示例

/**
 * Use block hooks to inject the custom block after the heading block
 *
 * @param string                          $hooked_blocks The list of hooked block types.
 * @param string                          $position The relative position of the hooked blocks. Values: 'before', 'after', 'first_child', or 'last_child'.
 * @param string                          $anchor_block The anchor block type.
 * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, or pattern that the anchor block belongs to.
 * @return string
 */
function hook_custom_text_after_block( $hooked_blocks, $position, $anchor_block, $context ) {
    // Check if we're in the 'after' position.
    if ( $position !== 'after' ) {
        return $hooked_blocks;
    }
    
    // Check if the anchor block is the heading block.
    if ( $anchor_block !== 'core/heading' ) {
        return $hooked_blocks;
    }
    
    // Add our custom block to be hooked after heading block.
    $hooked_blocks[] = 'custom/flash-text';
    
    return $hooked_blocks;
}
add_filter( 'hooked_block_types', 'hook_custom_text_after_block', 10, 4 );

📄 原文内容

Filters the list of hooked block types for a given anchor block type and relative position.

Parameters

$hooked_block_typesstring[]
The list of hooked block types.
$relative_positionstring
The relative position of the hooked blocks.
Can be one of 'before', 'after', 'first_child', or 'last_child'.
$anchor_block_typestring
The anchor block type.
$contextWP_Block_Template|WP_Post|array
The block template, template part, post object, or pattern that the anchor block belongs to.

Source

$hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context );

Changelog

Version Description
6.4.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    The following example will insert a custom block after after heading block:

    /**
     * Use block hooks to inject the custom block after the heading block
     *
     * @param string                          $hooked_blocks The list of hooked block types.
     * @param string                          $position The relative position of the hooked blocks. Values: 'before', 'after', 'first_child', or 'last_child'.
     * @param string                          $anchor_block The anchor block type.
     * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, or pattern that the anchor block belongs to.
     * @return string
     */
    function hook_custom_text_after_block( $hooked_blocks, $position, $anchor_block, $context ) {
    	// Check if we're in the 'after' position.
    	if ( $position !== 'after' ) {
    		return $hooked_blocks;
    	}
    	
    	// Check if the anchor block is the heading block.
    	if ( $anchor_block !== 'core/heading' ) {
    		return $hooked_blocks;
    	}
    	
    	// Add our custom block to be hooked after heading block.
    	$hooked_blocks[] = 'custom/flash-text';
    	
    	return $hooked_blocks;
    }
    add_filter( 'hooked_block_types', 'hook_custom_text_after_block', 10, 4 );