2

I am creating a plugin that is supposed to add custom blocks to Gutenberg. I have registered and built all my blocks using WordPress's JavaScript function wp.blocks.registerBlockType(). It works just as I expected, but recently I found out that WordPress also has a PHP function called register_block_type().

The documentation says register_block_type() "registers a block type".. It uses the WP_Block_Type_Registry class, but I don't really understand what the class does.

Anyone could help me understand what register_block_type() does exactly and what is the difference with wp.blocks.registerBlockType()?

Thanks for your help!

0

The documentation says register_block_type() "registers a block type"..

Yes, it does the same job but in PHP rather than javascript.

register_block_type can also be used to load in JS blocks. You can register a block and declare the CSS/JS/etc files to load.

For example, see this code snippet posted in the comments of the official documentation page:

    register_block_type( 'mcqac/related-quiz', array(
        'editor_script' => 'mcqac-related-quiz-block-script',
        'editor_style'  => 'mcqac-related-quiz-block-editor-style',
        'style'         => 'mcqac-related-quiz-block-frontend-style',
    ) );

Alternatively you could provide a render callback and render the block server side in PHP.

It uses the WP_Block_Type_Registry class, but I don't really understand what the class does.

Not quite, it uses WP_Block_Type. It has to store that data somewhere, rather than storing it as an array with no definition, it stores it as a structured object.

This object provides some helper methods for dealing with blocks of that type, as well as helper methods for rendering blocks of that type if you pass in the given attributes. Although this only works for dynamic PHP rendered blocks.

WP_Block_Type_Registry is just the class type of the object that holds all of the block types. A similar structure exists in JS.

Similar class types exist for every major object type in WordPress, e.g. WP_Term WP_Theme, or WP_Post_Type. As with most classes in WordPress, it isn't about what they do, but what they are/contain. Most WP core objects you will deal with are data containers of some form with a handful of helper methods, or, they're query objects such as WP_Query.

Anyone could help me understand what register_block_type() does exactly and what is the difference with wp.blocks.registerBlockType()?

One is PHP the other is JS, they're intended to do the same thing, register a block. It's just that sometimes you need to render things server side, so a way to register things in PHP is desirable.

2
  • So if I understand correctly, you would use the PHP method if you have some server-side processing to do before rendering your block, and the JS method if it is not the case. Correct? – Simon Lac Sep 16 '20 at 13:56
  • 1
    That's about right yes, but it is possible to render a block server side without using this function. This function also ensures that the block appears in the REST API. Given that your block currently works with just JS, I'd keep things as i – Tom J Nowell Sep 16 '20 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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