I've made a Gutenberg block which is only appropriate to show on hierarchical post types, so I want to disable it from the block inserter for non-hierarchical post types.

I thought about how to achieve this in PHP, but the WordPress documentation on registering blocks seems to suggest that blocks should be registered in PHP on the init hook and not a later hook where you could check if the post being edited is hierarchical. At the time the init hook fires, the currently viewed post is not yet available via e.g. get_post(), so I can't check its post_type.

I also thought about how to achieve this in the (JavaScript) script that I register with the editor_script property passed to register_block_type, but I don't know how to check the post type being edited to make it possible to halt the block registration in the case of non-hierarchical post types. It also seems a bit silly to enqueue and run the block script only for the script to decide that it shouldn't do anything, so ideally I would like to implement this checking in PHP.

Is there some standard way to make a block available only for specific post types, ideally in PHP?


You can use the allowed_block_types hook:

function wpse_allowed_block_types($allowed_block_types, $post) {
    // Limit blocks in 'post' post type
    if($post->post_type == 'post') {
        // Return an array containing the allowed block types
        return array(
    // Limit blocks in 'page' post type
    elseif($post->post_type == 'page') {
        return array(
    // Allow defaults in all other post types
    else {
        return $allowed_block_types;
add_filter('allowed_block_types', 'wpse_allowed_block_types', 10, 2);

Tweak with as many or as few conditions as you need. You can also enable non-Core blocks by identifying their namespace and name and putting those in the array.

| improve this answer | |
  • Thank you, but is there a way though to explicitly disallow a block, instead of having to provide a whitelist? As far as I understand this filter provides true by default, i.e. all blocks, instead of an actual list of block names which I could remove mine from. I'd rather not have to update my plugin every time a new core block is added, or remove other plugins' blocks by not adding them to my whitelist this sort of function. – Sean Oct 3 '19 at 16:43
  • 1
    As of January there was not yet a way to blacklist. See related wordpress.stackexchange.com/questions/326959/… – WebElaine Oct 3 '19 at 17:15
  • 1
    Thanks. That's unfortunate. I hope the developers add some whitelist mechanism in the future. – Sean Oct 4 '19 at 7:25
  • Thank you for this. EXACTLY what I was looking for & totally nailed my use-case. – indextwo Sep 11 at 19:16

If you need to whitelist the blocks that you want to use on your post you can use this method from the WordPress handbook https://developer.wordpress.org/block-editor/developers/filters/block-filters/#using-a-whitelist

// my-plugin.js
var allowedBlocks = [

wp.blocks.getBlockTypes().forEach( function( blockType ) {
    if ( allowedBlocks.indexOf( blockType.name ) === -1 ) {
        wp.blocks.unregisterBlockType( blockType.name );
} );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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