首页 > 解决方案 > ServerSideRender 属性不传递给后端

问题描述

我正在使用 @wordpress/server-side-render 从后端获取 Gutenberg 块的内容。

if ( props && props.attributes ) {
    blockContent = <ServerSideRender
        block="test/employee-block"
        attributes={ props.attributes }
    />
}

这是块的PHP端

register_block_type( 'test/employee-block', array(
        'title' => 'Test Block',
        'description' => 'test',
        'category' => 'widgets',
        'icon' => 'admin-users',
        'api_version' => 2,
        'editor_script' => 'employees_dynamic_block_script',
        'render_callback' => function ($block_attributes, $content) {
            return '<h1>test</h1>';
        }
    ) );

但是,渲染回调函数的 $block_attributes 始终为空。我不知道为什么,但根据API 文档,它应该在那里。

标签: phpreactjswordpresswordpress-gutenberggutenberg-blocks

解决方案


找到了解决方法,如果您没有在 register_block_type 函数中定义参数键和类型,它不会将它们传递给渲染回调。

register_block_type( 'test/employee-block', array(
        'api_version' => 2,
        'editor_script' => 'employees_dynamic_block_script',
        'attributes'      => array(
            'selectControl'             => array(
                'type' => 'string',
                'default' => '0',
            )
        ),
        'render_callback' => function ($block_attributes, $content) {
            return '<h1>test</h1>';
        }
    ) );

推荐阅读