如何删除WordPress 5.6中的核心嵌入块?

时间:2020-12-10 作者:Sven

跟进this question, 我想删除WordPress块编辑器中的一些默认(核心)嵌入块。从WordPress 5.6开始,在core-embed/* 命名空间。

如何注销单个core嵌入块?

2 个回复
最合适的回答,由SO网友:Sven 整理而成

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);
    }
  });
});
SO网友:Marc

作为一名主题开发人员,我经常希望嵌入块仅限于youtube和vimeo,就像Sven一样。下面是Sven的回答:在我的php代码中:

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\' );
在我的新javascript文件中,拒绝列表块。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]);
    }
} );
请注意vimeoyoutube 已注释。然而,这应该是一种更好的方法,例如禁用一行中的所有变体,然后只启用所需的变体。

还值得注意的是,使用allowed_block_types 将wordpress更新为5.6时,必须修改禁用嵌入的过滤器。