3

I'm using @wordpress/server-side-render to get the content of the Gutenberg block from the backend side.

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

and here is the PHP side of the block

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>';
        }
    ) );

However, the $block_attributes of the render callback function is always empty. I don't know why but according to the API documentation, it should be there.

Alper BULUT
  • 284
  • 2
  • 8
  • Does this answer your question? [Wordpress Custom Gutenberg Block render\_callback doesnt render](https://stackoverflow.com/questions/65592336/wordpress-custom-gutenberg-block-render-callback-doesnt-render) – Derple Sep 19 '21 at 22:48
  • No but I found the solution – Alper BULUT Sep 20 '21 at 07:16

1 Answers1

3

Found the fix, if you do not define the argument key and type in the register_block_type function, it does not pass them to the render callback.

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>';
        }
    ) );
Alper BULUT
  • 284
  • 2
  • 8