I am currently registering custom blocks via javascript only. I am building a site with lots of custom layouts and widgets, and I'm using the new custom blocks of Gutenberg to facilitate content creation using these custom layouts.
I have a few Custom Blocks, but I did not want to create a Plugin for each block, so I decided to group all the blocks together inside my theme. I created directory for each block, and each directory has an block.js file, which registers the block via the javascript function wp.blocks.registerBlockType
.
Sample block.js
const { registerBlockType } = wp.blocks;
registerBlockType( 'myguten/test-block', {
title: 'Basic Example',
icon: 'smiley',
category: 'layout',
edit() {
return <div>Hola, mundo!</div>;
},
save() {
return <div>Hola, mundo!</div>;
},
} );
Here is the sample folder structure for my blocks:
/my-theme
/src
/blocks
/a-custom-block
block.js
styles.scss
/another-custom-block
block.js
styles.scss
index.js
functions.php
webpack.config.js
I have webpack configured to bundle all my block.js
files into one file called dist/blocks.build.js
. And in my functions.php, I load my blocks.build
like so:
function enqueue_custom_blocks() {
wp_enqueue_script('custom_blocks', get_stylesheet_directory_uri() . '/dist/blocks.js', array('wp-blocks'));
}
add_action( 'enqueue_block_editor_assets', 'enqueue_custom_blocks');
This setup totally works. I am able to work on my custom blocks right in my theme folder, and I have no problem using these custom blocks on my pages and posts.
Therefore, I am curious, what is the purpose of the php function register_block_type
? Furthermore, is there anyway I could use register_block_type
to register blocks in bulk? My only real reason for wanting to use register_block_type
is to leverage the render_callback
option if, for example, [my block needed to be dynamic and leverage server-side rendering. Can I apply a filter to a block?