Restrict available Gutenberg blocks by post type to simplify editing, improve consistency, and prevent clients from inserting unnecessary layout blocks.
Use this snippet when building client websites where editors should only use a curated set of blocks.
For a custom ‘portfolio‘ post type, editors usually only need a limited set of blocks to maintain consistency and avoid misuse of layouts.
A typical portfolio entry may include:
—
=> Project title
=> Short description
=> Portfolio gallery
=> Project links
=> Category terms
=> Key highlights/features
Instead of allowing every Gutenberg block, we can create a cleaner editing experience by allowing only the blocks needed for structured portfolio content.
/**
* Limit allowed Gutenberg blocks by post type.
* Creates a cleaner editing experience for specific post types.
*/
add_filter( 'allowed_block_types_all', 'wpfolks_limit_blocks_by_post_type', 10, 2 );
function wpfolks_limit_blocks_by_post_type( $allowed_blocks, $editor_context ) {
if ( empty( $editor_context->post ) ) {
return $allowed_blocks;
}
$post_type = $editor_context->post->post_type;
if ( 'post' === $post_type ) {
return array(
'core/paragraph',
'core/heading',
'core/list',
'core/image',
'core/quote',
'core/separator',
);
}
if ( 'page' === $post_type ) {
return array(
'core/paragraph',
'core/heading',
'core/image',
'core/columns',
'core/group',
'core/buttons',
'core/spacer',
);
}
if ( 'portfolio' === $post_type ) {
return array(
'core/paragraph',
'core/heading',
'core/list',
'core/image',
'core/gallery',
'core/buttons',
'core/separator',
'core/group',
);
}
return $allowed_blocks;
}