最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

php - Removing Default Gutenberg Blocks, But Keeping Reusable Block Functionality?

programmeradmin1浏览0评论

I have been follow a tutorial here: .html Where it discusses how to remove default Gutenberg blocks like so:

add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );


function misha_allowed_block_types( $allowed_blocks, $post ) {


$allowed_blocks = array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
);

if( $post->post_type === 'page' ) {
    $allowed_blocks[] = 'core/shortcode';
}

return $allowed_blocks;


}

So what this is doing is limiting the default Gutenberg blocks to image, paragraph, heading, and list. I then set the page post type to also allow for the shortcode block. So this works fine, but I am now having an issue where it removes the functionality of reusable blocks and I can't figure out what needs to be added to add that functionality back.

I tried commenting on that article, but the author wasn't able to think of a solution currently. I was wondering if anyone had any ideas on adding the reusable block functionality back after adding the above code snippet?

I have been follow a tutorial here: https://rudrastyh.com/gutenberg/remove-default-blocks.html Where it discusses how to remove default Gutenberg blocks like so:

add_filter( 'allowed_block_types', 'misha_allowed_block_types', 10, 2 );


function misha_allowed_block_types( $allowed_blocks, $post ) {


$allowed_blocks = array(
    'core/image',
    'core/paragraph',
    'core/heading',
    'core/list'
);

if( $post->post_type === 'page' ) {
    $allowed_blocks[] = 'core/shortcode';
}

return $allowed_blocks;


}

So what this is doing is limiting the default Gutenberg blocks to image, paragraph, heading, and list. I then set the page post type to also allow for the shortcode block. So this works fine, but I am now having an issue where it removes the functionality of reusable blocks and I can't figure out what needs to be added to add that functionality back.

I tried commenting on that article, but the author wasn't able to think of a solution currently. I was wondering if anyone had any ideas on adding the reusable block functionality back after adding the above code snippet?

Share Improve this question asked Jan 7, 2019 at 16:53 user9664977user9664977 3775 silver badges12 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 12

The reusable block is registered with the core/block name.

I tried to add it to the allowed blocks in the allowed_block_types filter, here's an example:

add_filter( 'allowed_block_types', 'wpse324908_allowed_block_types', 10, 2 );

function wpse324908_allowed_block_types( $allowed_blocks, $post ) {    
   $allowed_blocks = array(
        'core/block', // <-- Include to show reusable blocks in the block inserter.
        'core/image',
        'core/paragraph',
    );     
    return $allowed_blocks;    
}

and it showed the reusable blocks within the block inserter, if at least two other blocks were included as well:

Looking at the /wp-includes/js/dist/editor.js we can e.g. see this check for core/block regarding including reusable blocks in the block inserter:

var selectors_canIncludeReusableBlockInInserter = function canIncludeReusableBlockInInserter(state, reusableBlock, rootClientId) {

  if (!selectors_canInsertBlockTypeUnmemoized(state, 'core/block', rootClientId)) {
    return false;
  }

As of WP 5.8, allowed_block_types is deprecated. So instead, we use allowed_block_types_all.

Example usage:

function cc_gutenberg_allowed_blocks($block_editor_context, $editor_context) {
    if ( !empty( $editor_context->post ) ) {

        // allow these blocks
        $allowed_blocks = array(
            'core/image',
            'core/paragraph',
            'core/heading',
        );

        // target specific post type
        if ( $editor_context->post->post_type === 'page' ) {
            $allowed_blocks[] = 'core/shortcode';
        }

        return $allowed_blocks;

    }

    return $block_editor_context;
}
add_filter( 'allowed_block_types_all', 'cc_gutenberg_allowed_blocks', 10, 2 );
发布评论

评论列表(0)

  1. 暂无评论