1

In gutenberg/block-editor, how can I check whether I've already registered a block type? Is there a function I can use? Searching through the Block Editor Handbook I couldn't see a function to check this.

An example of what I am trying to do is below:

class My_Block {

    public function __construct() {
        if ( ! SOME_FUNCTION_block_exists('foo/column') ) {
            register_block_type( 'foo/column', my_args );
        }
    }
    
}
2
  • 1
    have you encountered problems with blocks being registered multiple times? What's the reason and context behind the question?
    – Tom J Nowell
    Mar 3, 2022 at 1:40
  • @TomJNowell I haven't tested what happens when a block is registered twice. The reason is for efficiency of code.
    – sazr
    Mar 3, 2022 at 8:58

2 Answers 2

3

EDIT

I just realized you're doing this on the PHP side. There is a class called WP_Block_Type_Registry that you can use to see what is already registered:

$registry = WP_Block_Type_Registry::get_instance();
if ( ! $registry->get_registered( 'foo/column' ) ) {
    // YOUR CODE
}

Gutenberg should fire a console error if a block has already been registered. See the registerBlockType src here.

0

When editing a page using Gutenberg, you can use the Developers Console to execute wp.blocks.getBlockTypes() and get all active registered blocks in Gutenberg.

You can also use the WP_Block_Type_Registry Class in order get all server-side registered Blocks. You can than either use get_all_registered() or get_instance():

WP_Block_Type_Registry::get_all_registered();

or

WP_Block_Type_Registry::get_instance()->get_all_registered();

Both do the same. get_instance() returns itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.