4

I want to hide some blocks based on the post type. At first, I thought it's possible to hide them by going to Editor > Preferences > Blocks > Visible blocks and toggle them, but noticed that doing this only hides those blocks for myself, in my user, and not for everyone (I'm admin).

I also tried "Block Manager" plugin, but it hides the blocks for all post types. For example I want block A in post type A but not in post type B, and using this plugin doesn't help.

Is there a custom function, plugin, or setting to handle this (Disabling specific blocks for specific post types for every user/editor)?

2 Answers 2

9

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:

  • core/image -Image

  • core/gallery -Gallery

  • core/audio -Audio

  • core/cover -Cover

  • core/file -File

  • core/media-text -Media & Text

  • core/video -Video

Design category:

  • core/buttons -Buttons

  • core/columns -Columns

  • core/group -Group

  • core/row -Row

  • core/stack -Stack

  • core/more -More

  • core/nextpage -Page Break

  • core/separator -Separator

  • core/spacer -Spacer

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:

  • core/embed -Embed
6
  • 1
    Thank you for the complete explanation. BTW, I searched a lot, and seems the only way to do this is by whitelisting the blocks I want, not blacklisting the ones I don't want. So, is there a way to whitelist blocks made by other plugins?
    – Shahriar
    Commented Oct 9, 2022 at 22:11
  • 2
    You would have to know the slug of the 3rd party plug-in. You could run this function get_allowed_block_types and see the list. Make sure that you disable the function you created in this question though or the get_allowed_block_types will only return your modified array. Once you have the new slugs, add them to your allowed_block_types_all hook.
    – rudtek
    Commented Oct 10, 2022 at 15:09
  • Thank you so much!
    – Shahriar
    Commented Oct 10, 2022 at 15:12
  • First, running get_allowed_block_types will simply return true if filter is not used. As a sidenote, you cannot run get_allowed_block_types inside allowed_block_types_all, since it uses allowed_block_types_all hook, so you would create infinite loop
    – Lovor
    Commented Apr 2, 2023 at 12:12
  • 1
    You can also temporarily enable all block types (return true), then head over to the wp-admin editor and paste this in the console of the browser: wp.blocks.getBlockTypes().map(t=>t.name). Then you have a list of all block types and you can remove the ones you want to blacklist.
    – timlg07
    Commented Apr 17, 2023 at 7:53
0

To disable specific blocks, you can use the allowed_block_types_all hooks. Follow the approach below and make sure to replace the post type name for your needs.

<?php
function disable_specific_blocks( $allowed_block_types, $post ) {
    // An array of block names to disable
    $disabled_blocks = array(
        'core/table',
        'core/image',
        'core/gallery',
    );
  
    // Use this condition to disable blocks on certain post types, otherwise you can remove this IF condition
    if ( $post->post_type === 'post' ) {
        $allowed_block_types = array_diff( $allowed_block_types, $disabled_blocks );
    }

    return $allowed_block_types;
}
add_filter( 'allowed_block_types_all', 'disable_specific_blocks', 10, 2 );
?>

For complete guide, you can follow this tutorial on how to disable specific blocks on the WordPress block editor.

Your Answer

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

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