Hi @tsjippy
To change the type of a block programmatically in WordPress, you can use the update_block function provided by the WordPress block editor. This function allows you to modify the attributes of an existing block, including its type.
Here’s an example of how you could use this function to change the type of a block to a “vimeo” block:
function change_block_type( $block ) {
// Check if the block is a "video" block
if ( $block['blockName'] === 'core/video' ) {
// Change the block type to a "vimeo" block
$block['blockName'] = 'my-plugin/vimeo';
}
return $block;
}
add_filter( 'render_block', 'change_block_type' );
This code uses the render_block filter to modify the block before rendering it on the front end. It checks if the block is a “video” block, and if it is, it changes the block type to a “vimeo” block.
Remember that this will only change the block type on the front end, not in the block editor. To change the block type in the block editor, you will need to use JavaScript to modify the block.
You can use the wp.blocks.registerBlockType
function to register your “vimeo” block, and then use the wp.blocks.unregisterBlockType
function to unregister the “video” block. You can then use the wp.blocks.registerBlockType
function again to re-register the “video” block, but this time as a “vimeo” block.
Here’s an example of how you could do this:
wp.blocks.unregisterBlockType( 'core/video' );
wp.blocks.registerBlockType( 'core/video', {
// Define the block type as a "vimeo" block
name: 'core/vimeo',
// Other block type options...
} );
Remember that this will only work if the block is actually a “video” block and not some other type of block. You may need to modify the code to check for the specific block type you are trying to change.