函数文档

get_block_editor_server_block_settings()

💡 云策文档标注

概述

get_block_editor_server_block_settings() 函数用于为块编辑器准备服务器端注册的块数据,返回一个以块名称为键的关联数组,包含块在客户端注册所需的相关属性。

关键要点

  • 函数返回一个关联数组,键为块名称,值为块数据,用于块编辑器的客户端注册。
  • 数据提取自 WP_Block_Type_Registry,包括 apiVersion、title、description、icon、attributes、providesContext、usesContext、blockHooks、selectors、supports、category、styles、textdomain、parent、ancestor、keywords、example、variations、allowedBlocks 等字段。
  • 函数通过遍历所有已注册块,并筛选指定字段来构建返回数组。

代码示例

// Define a callback function to register block settings
function wpdocs_register_block_settings() {
    $block_type = 'core/paragraph'; // Replace with the block type you want to customize

    $settings = get_block_editor_server_block_settings( $block_type );

    // Modify the block settings as needed
    $settings['attributes']['customAttribute'] = array(
        'type'    => 'string',
        'default' => 'Custom Default Value',
    );

    // Re-register the block with the modified settings
    register_block_type( $block_type, $settings );
}

// Hook the callback function into the 'init' action
add_action( 'init', 'wpdocs_register_block_settings' );

注意事项

  • 函数在 WordPress 5.0.0 中引入,后续版本添加了新字段:6.3.0 添加 selectors,6.4.0 添加 block_hooks。
  • 相关函数包括 WP_Block_Type_Registry::get_instance() 和 WP_Customize_Widgets::enqueue_scripts()。

📄 原文内容

Prepares server-registered blocks for the block editor.

Description

Returns an associative array of registered block data keyed by block name. Data includes properties of a block relevant for client registration.

Return

array An associative array of registered block data.

Source

function get_block_editor_server_block_settings() {
	$block_registry = WP_Block_Type_Registry::get_instance();
	$blocks         = array();
	$fields_to_pick = array(
		'api_version'      => 'apiVersion',
		'title'            => 'title',
		'description'      => 'description',
		'icon'             => 'icon',
		'attributes'       => 'attributes',
		'provides_context' => 'providesContext',
		'uses_context'     => 'usesContext',
		'block_hooks'      => 'blockHooks',
		'selectors'        => 'selectors',
		'supports'         => 'supports',
		'category'         => 'category',
		'styles'           => 'styles',
		'textdomain'       => 'textdomain',
		'parent'           => 'parent',
		'ancestor'         => 'ancestor',
		'keywords'         => 'keywords',
		'example'          => 'example',
		'variations'       => 'variations',
		'allowed_blocks'   => 'allowedBlocks',
	);

	foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) {
		foreach ( $fields_to_pick as $field => $key ) {
			if ( ! isset( $block_type->{ $field } ) ) {
				continue;
			}

			if ( ! isset( $blocks[ $block_name ] ) ) {
				$blocks[ $block_name ] = array();
			}

			$blocks[ $block_name ][ $key ] = $block_type->{ $field };
		}
	}

	return $blocks;
}

Changelog

Version Description
6.4.0 Added block_hooks field.
6.3.0 Added selectors field.
5.0.0 Introduced.

User Contributed Notes

  1. Skip to note 2 content

    // Define a callback function to register block settings
    function wpdocs_register_block_settings() {
        $block_type = 'core/paragraph'; // Replace with the block type you want to customize
    
        $settings = get_block_editor_server_block_settings( $block_type );
    
        // Modify the block settings as needed
        $settings['attributes']['customAttribute'] = array(
            'type'    => 'string',
            'default' => 'Custom Default Value',
        );
    
        // Re-register the block with the modified settings
        register_block_type( $block_type, $settings );
    }
    
    // Hook the callback function into the 'init' action
    add_action( 'init', 'wpdocs_register_block_settings' );