In PHP, it's certainly possible to check if a block type is dynamic or whether the block uses server-side rendering. For example for already registered block types, just get the block type object and call its is_dynamic
method.
$block_registry = WP_Block_Type_Registry::get_instance();
$block_name = 'core/latest-posts';
$block_type_object = $block_registry->get_registered( $block_name );
if ( $block_type_object ) {
// Note: render_callback is not included in the REST API response.
var_dump(
$block_name,
$block_type_object->is_dynamic(),
$block_type_object->render_callback
);
}
In Gutenberg though, for now you would need to fetch the block type object/data from the Block Types API endpoints, and just check whether the block type's is_dynamic
property is true
.
But you could combine PHP and JS: Add a constant/variable which stores the names of the dynamic block types, and in checkServersideRender()
, just check whether settings.name
is in the list.
Example using get_dynamic_block_names()
and wp_add_inline_script()
:
PHP:
add_action( 'enqueue_block_editor_assets', 'my_enqueue_block_editor_assets' );
function my_enqueue_block_editor_assets() {
wp_add_inline_script(
'wp-blocks',
sprintf(
'const DYNAMIC_BLOCKS = %s;',
wp_json_encode( get_dynamic_block_names() )
)
);
}
JS:
const checkServersideRender = ( settings ) => {
if ( DYNAMIC_BLOCKS.indexOf( settings.name ) >= 0 ) {
// it's a dynamic block type; add your attributes
}
return settings;
};
But then, for what you're trying to do, it's probably better/easier to just add your custom attributes using the register_block_type_args
hook. That way, you wouldn't need to worry about breaking server-side rendered blocks, or that you would be free from the rest_invalid_param
error as stated in the previous revision.
add_filter( 'register_block_type_args', 'my_register_block_type_args' );
function my_register_block_type_args( $args ) {
$args['attributes']['your_attribute'] = array(
'type' => 'boolean',
'default' => false,
}
return $args;
}