Skip to content

Commit

Permalink
Add ESNext example for unregisterBlockType
Browse files Browse the repository at this point in the history
Add an explanation why 'wp-edit-post' is needed to unregister a block
  • Loading branch information
grappler committed Mar 11, 2020
1 parent f5bfffa commit ca0b4bb
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions docs/designers-developers/developers/filters/block-filters.md
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,25 @@ wp.hooks.addFilter( 'editor.BlockListBlock', 'my-plugin/with-client-id-class-nam

Adding blocks is easy enough, removing them is as easy. Plugin or theme authors have the possibility to "unregister" blocks.

{% codetabs %}
{% ES5 %}
```js
// my-plugin.js
wp.domReady( function() {
wp.blocks.unregisterBlockType( 'core/verse' );
} );
```
{% ESNext %}
```js
// my-plugin.js
import { unregisterBlockType } from '@wordpress/blocks';
import domReady from '@wordpress/dom-ready'

domReady( function() {
unregisterBlockType( 'core/verse' );
} );
```
{% end %}

and load this script in the Editor

Expand All @@ -347,6 +360,8 @@ function my_plugin_blacklist_blocks() {
add_action( 'enqueue_block_editor_assets', 'my_plugin_blacklist_blocks' );
```

**Important:** When unregistering a block, there can be a [race condition](https://en.wikipedia.org/wiki/Race_condition) on which code runs first: registering the block, or unregistering the block. You want your unregister code to run last. The way to do that is specify the component that is registering the block as a dependency, in this case `wp-edit-post`. Additionally, using `wp.domReady()` ensures the unregister code runs once the dom is loaded.

### Using a whitelist

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

0 comments on commit ca0b4bb

Please sign in to comment.