admin管理员组

文章数量:1122826

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?

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?

Share Improve this question edited Dec 10, 2020 at 10:41 Sven asked Dec 10, 2020 at 10:05 SvenSven 3,6641 gold badge34 silver badges48 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 18

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
bluesky
cloudup
collegehumor
crowdsignal
dailymotion
facebook
flickr
imgur
instagram
issuu
kickstarter
meetup-com
mixcloud
pinterest
pocket-casts
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);
    }
  });
});

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.

本文标签: wp adminHow to remove the core embed blocks in WordPress 56