diff --git a/docs/extensibility/extending-blocks.md b/docs/extensibility/extending-blocks.md index 106afa5ad7e4e..bbbb709350c81 100644 --- a/docs/extensibility/extending-blocks.md +++ b/docs/extensibility/extending-blocks.md @@ -120,10 +120,13 @@ wp.blocks.getBlockTypes().forEach( function( blockType ) { ## Hiding blocks from the inserter -On the server, you can filter the list of blocks shown in the inserter using the `allowed_block_types` filter. you can return either true (all block types supported), false (no block types supported), or an array of block type names to allow. +On the server, you can filter the list of blocks shown in the inserter using the `allowed_block_types` filter. You can return either true (all block types supported), false (no block types supported), or an array of block type names to allow. You can also use the second provided param `$post` to filter block types based on its content. ```php -add_filter( 'allowed_block_types', function() { +add_filter( 'allowed_block_types', function( $allowed_block_types, $post ) { + if ( $post->post_type === 'post' ) { + return $allowed_block_types; + } return [ 'core/paragraph' ]; -} ); +}, 10, 2 ); ``` diff --git a/lib/client-assets.php b/lib/client-assets.php index 0cf66a5e4c71f..bb52445e67ead 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -937,8 +937,9 @@ function gutenberg_editor_scripts_and_styles( $hook ) { * * @param bool|array $allowed_block_types Array of block type slugs, or * boolean to enable/disable all. + * @param object $post The post resource data. */ - $allowed_block_types = apply_filters( 'allowed_block_types', true ); + $allowed_block_types = apply_filters( 'allowed_block_types', true, $post ); $editor_settings = array( 'alignWide' => $align_wide || ! empty( $gutenberg_theme_support[0]['wide-images'] ), // Backcompat. Use `align-wide` outside of `gutenberg` array.