Restrict Allowed Blocks in Post Type to single Embed

Question

I am trying to limit the allowable blocks in a custom post type to just a YouTube video block. It appears there may have been some changes in 5.6 because this used to work:

/**
 * Limit blocks to the following for Video
 *
 * @param array|bool $allowed_block_types Allowed Block Types.
 * @param WP_Post    $post                Current Post.
 *
 * @return string[]|bool
 */
function barasch_video_allowed_blocks( $allowed_block_types, WP_Post $post ) {
    if ( 'video' !== $post->post_type ) {
        return $allowed_block_types;
    }
    return [ 'core-embed/youtube' ];
}

add_filter( 'allowed_block_types', 'barasch_video_allowed_blocks', 10, 2 );

As it currently stands, you can’t add any blocks because core-embed/youtube appears to no longer be recognized when specified in allowable blocks. If I change core-embed/youtube to core/embed, that allows the blocks to reappear but I see all available embed blocks which I’d like to avoid if possible.

Strangely, when registering the custom post type, the YouTube block does appear as shown below if I specify it within the template, so I’m at a loss as to what is going on. It appears that the core-embed/youtube slug is recognized when declaring the template, but not when specifying allowed blocks.

register_post_type(
    'video',
    [
        .... remaining arguments ....
        'template'            => [
            [
                'core-embed/youtube',
                [
                    'placeholder' => __( 'Add Video here...', 'barasch' ),
                ],
            ],
        ],
    ]
);

}

enter image description here

0
Phil Birnie 3 weeks 0 Answers 8 views 0

Leave an answer