5

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?

2 Answers 2

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:

Reusable

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;
  }
2
  • Thanks, I've been searching for this everywhere!
    – Johannes
    Nov 4, 2021 at 17:59
  • Glad to hear it helped
    – birgire
    Nov 4, 2021 at 23:09
-1

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 );
1
  • This should be a comment on the question instead of an answer, as it doesn't address the question.
    – alpipego
    May 13 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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