How to restrict access to the core Gutenberg blocks associated with the specified custom post type?

Uncategorized
/*
This restricts the custom post types'sponsors,' 'news,' and 'faqs' to the basic Gutenberg blocks mentioned.
*/

function wpdocs_allowed_post_type_blocks( $allowed_block_types, $editor_context ) {
	if ( 'sponsors' === $editor_context->post->post_type ) {
		return array(
			'core/paragraph',
		);
	}

	if ( 'news' === $editor_context->post->post_type ) {
		return array(
			'core/paragraph',
			'core/list',
			'core/image',
			'core/buttons',
			'core/quote',
		);
	}

	if ( 'faqs' === $editor_context->post->post_type ) {
		return array(
			'core/paragraph',
			'core/list',
			'core/image',
			'core/buttons',
		);
	}

	return $blocks;
}

add_filter( 'allowed_block_types_all', 'wpdocs_allowed_post_type_blocks', 10, 2 );

Leave a Reply

Your email address will not be published. Required fields are marked *