@birgire 's answer is very good.
https://wordpress.stackexchange.com/a/326969/198298
Some information might be missing though.
As @birgire guesses, it really is necessary to wrap the code inside a
wp.domReady( function() {
// ...
} );
even if the script is loaded with the dependencies array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' )
like it is recommended in the block filters guide.
Speaking of loading: If you're no plugin developer (like me) but only want to remove some block types for the users of your (company's) blog, you should load
the js file for this in your child-theme's functions.php
like that:
/** Allow js-side adjustments to block editor (aka Gutenberg), i.e. remove certain block types. */
function my_childtheme_enqueue_block_editor_adj() {
wp_enqueue_script(
'block-editor-adjustments',
get_stylesheet_directory_uri() . '/js/block-editor-adjustments.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
);
}
add_action( 'enqueue_block_editor_assets', 'my_childtheme_enqueue_block_editor_adj' );
My js file looks like this:
wp.domReady(function () {
/**
* Remove some blocks from the block editor.
* This is not possible in php unfortunately.
*/
wp.blocks.unregisterBlockType('core/embed');
var allowedEmbedBlocks = [
'core-embed/twitter',
'core-embed/youtube',
'core-embed/facebook',
'core-embed/instagram',
'core-embed/wordpress',
'core-embed/flickr',
'core-embed/vimeo',
];
wp.blocks.getBlockTypes().forEach(function (blockType) {
if (blockType.name.startsWith('core-embed')
&& allowedEmbedBlocks.indexOf(blockType.name) === -1
) {
wp.blocks.unregisterBlockType(blockType.name);
}
});
});
This way I can now focus on the embed options in allowedEmbedBlocks
regarding the Usercentrics data privacy settings.