Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extensibility: Pass the post object to allowed_block_types filter #6208

Merged
merged 1 commit into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions docs/extensibility/extending-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
```
3 changes: 2 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down