8

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?

13

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
reddit
reverbnation
screencast
scribd
slideshare
smugmug
soundcloud
speaker-deck
spotify
ted
tiktok
tumblr
twitter
videopress
vimeo
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);
    }
  });
});
2
  • 1
    Do you have to unregister each variation individually or can you just unregister the block itself? And instead of hardcoding, why not use the result from wp.blocks.getBlockVariations('core/embed')? – kero Dec 10 '20 at 10:07
  • 2
    @kero Removing the embed block also removes all the variations (see updated answer above). It might be useful to keep the embed block and just remove individual variations. Maybe I want to embed videos (from YouTube and Vimeo) but get rid of all the other embeds. – Sven Dec 10 '20 at 10:22
3

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.

6
  • Thank you! For me, the working solution was a combination of your answer and @Sven's answer: I took the PHP part from your answer (in functions.php) and used the very last JS code block from Sven's answer as the separate deny-list-blocks.js file which your PHP function links to. – Johannes Mar 3 at 22:20
  • @Marc what would it be a suitable alternative to allowed_block_types filter? – Giorgio25b Mar 6 at 0:21
  • @Johannes is there a link to your solution? – Giorgio25b Mar 6 at 0:24
  • @Giorgio25b Well, there are two public websites where I applied that solution, but as long as you don't have a user account for these (which won't be possible), you wouldn't be able to check any of that code or see which blocks are available for users. – Johannes Mar 6 at 0:30
  • @Johannes no, I meant is there a shared snippet, a gist, a POW, something to see your implemented code without having to share details of any of your production projects ;-) – Giorgio25b Mar 7 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.