How to disable blocks in Gutenberg editor for specific post type

Question

Put this in your Theme’s functions.php file.

add_filter( 'allowed_block_types_all', 'rt_allowed_block_types', 25, 2 );
 
function rt_allowed_block_types( $allowed_blocks, $editor_context ) {
    if( 'custom_post_type' === $editor_context->post->post_type ) { 
        $allowed_blocks = array(
        'core/image',
        'core/paragraph',
        'core/heading',
        'core/list'
        );
        return $allowed_blocks;
    } else {
        return;
    }
}

Here’s what it’s doing. The filter allowed_block_types_all is filtering what block types are allowed. We’re filtering that list based on the function rt_allowed_block_types. In the function we created an array of blocks that we want to use ONLY if we’re on the custom_post_type referenced in the function through the editor_context.

If you’re not on that custom post type, then no array is created so WordPress will just assume ALL blocks are allowed.

You will need to change custom_post_type to whatever post-type you want to have a filtered blocks list.

Next, the function has a list showing which blocks you want to be able to use on the selected post type.

I’m not sure which blocks you want to use so you’ll need to edit that part as well.

This is a reference for all (as of this right now) standard core blocks: (you’ll want to add just the slug…Not the name too.)

Text category:

  • core/paragraph -Paragraph

  • core/heading -Heading

  • core/list -List

  • core/preformatted -Preformatted

  • core/pullquote -Pullquote

  • core/table -Table

  • core/verse -Verse

Media category:

Design category:

Widgets category:

  • core/archives -Archive

  • core/calendar -Calendar

  • core/categories -Categories

  • core/html -Custom HTML

  • core/latest-comments -Latest Comments

  • core/latest-posts -Latest Posts

  • core/page-list -Page List

  • core/rss -RSS

  • core/search -Search

  • core/shortcode -Shortcode

  • core/social-links -Social Icons

  • core/tag-cloud -Tag Cloud

Theme category:

  • core/navigation -Navigation

  • core/site-logo -Site Logo

  • core/site-title -Site Title

  • core/site-tagline -Site Tagline

  • core/query -Query Loop

  • core/posts-list -Posts List

  • core/avatar -Avatar

  • core/post-title -Post Title

  • core/post-excerpt -Post Excerpt

  • core/post-featured-image -Post Featured Image

  • core/post-content -Post Content

  • core/post-author -Post Author

  • core/post-date -Post Date

  • core/post-terms -Post Categories,Post Tags

  • core/post-navigation-link -Next post,Previous post

  • core/read-more -Read More

  • core/comments-query-loop -Comments Query Loop

  • core/post-comments-form -Post Comments Form

  • core/loginout -Login/out

  • core/term-description -Term Description

  • core/query-title -Archive Title

  • core/post-author-biography -Post Author Biography

Embeds category:

0
Shahriar 4 weeks 2022-08-30T10:59:08-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse