How to remove the core embed blocks in WordPress 5.6?

Following up on this question, I want to remove some of the default (core) embed blocks in the WordPress block editor. As of WordPress 5.6, the blocks are no longer available under the core-embed/* namespace.

How can I unregister individual core embed blocks?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

With WordPress 5.6 (Gutenberg v8.8.0), the implementation of the core-embed/* blocks changed (see pull request #24090: Refactor embed block to single block with block variations). There are now 43 blocks with block variations of the core/embed block.

Available core blocks are:

core/paragraph
core/image
core/heading
core/gallery
core/list
core/quote
core/shortcode
core/archives
core/audio
core/button
core/buttons
core/calendar
core/categories
core/code
core/columns
core/column
core/cover
core/embed
core/file
core/group
core/freeform
core/html
core/media-text
core/latest-comments
core/latest-posts
core/missing
core/more
core/nextpage
core/preformatted
core/pullquote
core/rss
core/search
core/separator
core/block
core/social-links
core/social-link
core/spacer
core/subhead
core/table
core/tag-cloud
core/text-columns
core/verse
core/video

Unregister embeds altogether (including variations):

wp.domReady(function () {
  wp.blocks.unregisterBlockType('core/embed');
});

The blocks previously listed as core-embed/* are now available as a variation of core/embed:

console.table(wp.blocks.getBlockVariations('core/embed'));

Available block variations of core/embed are:

amazon-kindle
animoto
cloudup
collegehumor
crowdsignal
dailymotion
facebook
flickr
imgur
instagram
issuu
kickstarter
meetup-com
mixcloud
pinterest
reddit
reverbnation
screencast
scribd
slideshare
smugmug
soundcloud
speaker-deck
spotify
ted
tiktok
tumblr
twitter
videopress
vimeo
wolfram-cloud
wordpress
wordpress-tv
youtube

You can unregister a single variation like this:

wp.domReady(function () {
  wp.blocks.unregisterBlockVariation('core/embed', 'twitter');
});

Or unregister all variations and only allow individual variations:

wp.domReady(function () {
  const allowedEmbedBlocks = [
    'vimeo',
    'youtube',
  ];
  wp.blocks.getBlockVariations('core/embed').forEach(function (blockVariation) {
    if (-1 === allowedEmbedBlocks.indexOf(blockVariation.name)) {
      wp.blocks.unregisterBlockVariation('core/embed', blockVariation.name);
    }
  });
});

Method 2

As a theme developer I often want the embed blocks restricted to youtube and vimeo, just as Sven. So, following Sven’s answer:
In my php code:

function my_theme_deny_list_blocks() {
    wp_enqueue_script(
        'deny-list-blocks',
        get_template_directory_uri() . '/assets/js/deny-list-blocks.js',
        array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' )
    );
}
add_action( 'enqueue_block_editor_assets', 'my_theme_deny_list_blocks' );

In my new javascript file deny-list-blocks.js:

wp.domReady( function() {

    var embed_variations = [
                            'amazon-kindle',
                            'animoto',
                            'cloudup',
                            'collegehumor',
                            'crowdsignal',
                            'dailymotion',
                            'facebook',
                            'flickr',
                            'imgur',
                            'instagram',
                            'issuu',
                            'kickstarter',
                            'meetup-com',
                            'mixcloud',
                            'reddit',
                            'reverbnation',
                            'screencast',
                            'scribd',
                            'slideshare',
                            'smugmug',
                            'soundcloud',
                            'speaker-deck',
                            'spotify',
                            'ted',
                            'tiktok',
                            'tumblr',
                            'twitter',
                            'videopress',
                            //'vimeo'
                            'wordpress',
                            'wordpress-tv',
                            //'youtube'
                ];

    for (var i = embed_variations.length - 1; i >= 0; i--) {
        wp.blocks.unregisterBlockVariation('core/embed', embed_variations[i]);
    }
} );

Notice that vimeo and youtube are commented. Nevertheless, it should be a better way to do this, for instance disabling all variations in one line, then enabling only the desired ones.

Also worth noticing that all themes using the allowed_block_types filter to disable embeds will have to be modified when updating wordpress to 5.6.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x