Skip to content

Commit

Permalink
Docs: Improve code examples in extensibility docs
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jun 28, 2018
1 parent 93a3c91 commit e75c4f3
Showing 1 changed file with 21 additions and 11 deletions.
32 changes: 21 additions & 11 deletions docs/extensibility/extending-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-data-align', withDa
Adding blocks is easy enough, removing them is as easy. Plugin or theme authors have the possibility to "unregister" blocks.

```js
// myplugin.js
// myp-lugin.js

wp.blocks.unregisterBlockType( 'core/verse' );
```
Expand All @@ -151,24 +151,25 @@ and load this script in the Editor

```php
<?php
// myplugin.php
// my-plugin.php

function myplugin_blacklist_blocks() {
function my_plugin_blacklist_blocks() {
wp_enqueue_script(
'myplugin-blacklist-blocks',
plugins_url( 'myplugin.js', __FILE__ ),
'my-plugin-blacklist-blocks',
plugins_url( 'my-plugin.js', __FILE__ ),
array( 'wp-blocks' )
);
}
add_action( 'enqueue_block_editor_assets', 'myplugin_blacklist_blocks' );
add_action( 'enqueue_block_editor_assets', 'my_plugin_blacklist_blocks' );
```

### Using a whitelist

If you want to disable all blocks except a whitelisted list, you can adapt the script above like so:

```js
// myplugin.js
// my-plugin.js

var allowedBlocks = [
'core/paragraph',
'core/image',
Expand All @@ -188,20 +189,28 @@ wp.blocks.getBlockTypes().forEach( function( blockType ) {
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( $allowed_block_types, $post ) {
<?php
// my-plugin.php

function my_plugin_allowed_block_types( $allowed_block_types, $post ) {
if ( $post->post_type !== 'post' ) {
return $allowed_block_types;
}
return array( 'core/paragraph' );
}, 10, 2 );
}

add_filter( 'allowed_block_types', 'my_plugin_allowed_block_types', 10, 2 );
```

## Managing block categories

It is possible to filter the list of default block categories using the `block_categories` filter. You can do it on the server by implementing a function which returns a list of categories. It is going to be used during blocks registration and to group blocks in the inserter. You can also use the second provided param `$post` to generate a different list depending on the post's content.

```php
add_filter( 'block_categories', function( $categories, $post ) {
<?php
// my-plugin.php

function my_plugin_block_categories( $categories, $post ) {
if ( $post->post_type !== 'post' ) {
return $categories;
}
Expand All @@ -214,5 +223,6 @@ add_filter( 'block_categories', function( $categories, $post ) {
),
)
);
}, 10, 2 );
}
add_filter( 'block_categories', 'my_plugin_block_categories', 10, 2 );
```

0 comments on commit e75c4f3

Please sign in to comment.