钩子文档

allowed_block_types_all

💡 云策文档标注

概述

allowed_block_types_all 是一个 WordPress 过滤器,用于控制所有编辑器类型中允许使用的块类型。它允许开发者根据块编辑器上下文动态限制或允许特定块。

关键要点

  • 过滤器名称:allowed_block_types_all,用于过滤所有编辑器类型的允许块类型。
  • 参数:$allowed_block_types(布尔值或字符串数组,默认 true 表示支持所有已注册块类型)和 $block_editor_context(WP_Block_Editor_Context 对象,提供当前编辑器上下文)。
  • 用途:可用于基于文章类型等条件自定义允许的块列表,例如限制自定义文章类型仅使用特定核心块。
  • 引入版本:WordPress 5.8.0。

代码示例

function wpdocs_allowed_post_type_blocks( $allowed_block_types, $editor_context ) {
    if ( 'sponsors' === $editor_context->post->post_type ) {
        return array(
            'core/paragraph',
        );
    }

    if ( 'news' === $editor_context->post->post_type ) {
        return array(
            'core/paragraph',
            'core/list',
            'core/image',
            'core/buttons',
            'core/quote',
        );
    }

    if ( 'faqs' === $editor_context->post->post_type ) {
        return array(
            'core/paragraph',
            'core/list',
            'core/image',
            'core/buttons',
        );
    }

    return $allowed_block_types;
}

add_filter( 'allowed_block_types_all', 'wpdocs_allowed_post_type_blocks', 10, 2 );

注意事项

  • 核心块列表可参考官方文档以进行过滤。
  • 确保函数正确处理参数顺序和返回值,避免意外行为。

📄 原文内容

Filters the allowed block types for all editor types.

Parameters

$allowed_block_typesbool|string[]
Array of block type slugs, or boolean to enable/disable all.
Default true (all registered block types supported).
$block_editor_contextWP_Block_Editor_Context
The current block editor context.

Source

$allowed_block_types = apply_filters( 'allowed_block_types_all', $allowed_block_types, $block_editor_context );

Changelog

Version Description
5.8.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    How it can be used to filter for multiple post types in the same function. This filters the custom post types of ‘sponsors’, ‘news’ and ‘faqs’ to only allow the core Gutenberg blocks listed.

    function wpdocs_allowed_post_type_blocks( $allowed_block_types, $editor_context ) {
    	if ( 'sponsors' === $editor_context->post->post_type ) {
    		return array(
    			'core/paragraph',
    		);
    	}
    
    	if ( 'news' === $editor_context->post->post_type ) {
    		return array(
    			'core/paragraph',
    			'core/list',
    			'core/image',
    			'core/buttons',
    			'core/quote',
    		);
    	}
    
    	if ( 'faqs' === $editor_context->post->post_type ) {
    		return array(
    			'core/paragraph',
    			'core/list',
    			'core/image',
    			'core/buttons',
    		);
    	}
    
    	return $allowed_block_types;
    }
    
    add_filter( 'allowed_block_types_all', 'wpdocs_allowed_post_type_blocks', 10, 2 );

  2. Skip to note 6 content

    Basic Example

    function wpdocs_allowed_block_types ( $block_editor_context, $editor_context ) {
    	if ( ! empty( $editor_context->post ) ) {
    		return array(
    			'core/paragraph',
    			'core/heading',
    			'core/list',
    		);
    	}
    
    	return $block_editor_context;
    }
    
    add_filter( 'allowed_block_types_all', 'wpdocs_allowed_block_types', 10, 2 );