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.
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>';
}
) );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With