Search code examples
wordpresshookwordpress-gutenberg

How to removing certain core Gutenberg blocks on some Page Templates / Post Type


As per WordPress,

apply_filters_deprecated( 'allowed_block_types', bool|string[] $allowed_block_types, WP_Post $post )

Warning: This hook has been deprecated. Use the ‘allowed_block_types_all’ filter instead.

Below code is not working now.

add_filter('allowed_block_types', function($block_types, $post) {
  $allowed_blocks = [
    'core/shortcode',
    'core/image',
    'core/gallery',
    'core/heading',
    'core/quote',
  ];
  if ($post->post_type == 'page') {
    return $block_types;
  }
  else
  {
    $blocks_to_remove = [
      'acf/editor-intro',
    ];
    return array_diff($allowed_blocks, $blocks_to_remove);
  }
}, 10, 2);

Solution

  • You can use the below hook I have created some conditions like Post Types / Page Templates / etc.

    add_filter('allowed_block_types_all', function ( $block_editor_context, $editor_context ) {
        if ($editor_context->post_type == 'page') {
            // Here you can check any Post Types
            return array(
                'core/paragraph',
                'core/heading',
                'core/list',
                'core/quote', // Above are few example
                'acf/test-block' // you can allow disable custom ACF blocks also
                );
        } else if (basename(get_page_template()) == 'template-custom.php') {
            // Here you can check any Custom Templates
            return array(
                    'acf/test-block' // you can allow disable custom ACF blocks also
            );
        } else {
            // Here will return all default blocks
            return $block_editor_context;
        }
    }, 10, 2 );
    

    Below are some default blocks so you can remove as per required with hook.

    Block Types
    core/shortcode
    core/image
    core/gallery
    core/heading
    core/quote
    core/embed
    core/list
    core/separator
    core/more
    core/button
    core/pullquote
    core/table
    core/preformatted
    core/code
    core/html
    core/freeform
    core/latest-posts
    core/categories
    core/cover
    core/text-columns
    core/verse
    core/video
    core/audio
    core/block
    core/paragraph
    core-embed/twitter
    core-embed/youtube
    core-embed/facebook
    core-embed/instagram
    core-embed/wordpress
    core-embed/soundcloud
    core-embed/spotify
    core-embed/flickr
    core-embed/vimeo
    core-embed/animoto
    core-embed/cloudup
    core-embed/collegehumor
    core-embed/dailymotion
    core-embed/funnyordie
    core-embed/hulu
    core-embed/imgur
    core-embed/issuu
    core-embed/kickstarter
    core-embed/meetup-com
    core-embed/mixcloud
    core-embed/photobucket
    core-embed/polldaddy
    core-embed/reddit
    core-embed/reverbnation
    core-embed/screencast
    core-embed/scribd
    core-embed/slideshare
    core-embed/smugmug
    core-embed/speaker
    core-embed/ted
    core-embed/tumblr
    core-embed/videopress
    core-embed/wordpress-tv