Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ServerSideRender attributes does not passed to backend

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.

like image 935
Alper BULUT Avatar asked Dec 20 '25 10:12

Alper BULUT


1 Answers

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>';
        }
    ) );
like image 140
Alper BULUT Avatar answered Dec 23 '25 00:12

Alper BULUT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!