Custom WP Blocks (register_block_type) in a Parent Theme
I have a parent theme that was developed as a project base, and within that I have been building up a group of completely custom WP Blocks (scaffolded with npx wordpress user/create-block
).
These blocks work great when the parent theme is active, but if I activate a child theme, the blocks disappear.
The npx command creates a PHP file in the root with something like:
function create_block_custom_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'create_block_custom_block_init' );
And if I echo within that function the file path, the correct parent’s file path is returned and all looks well. Unfortunately, the block just doesn’t register or show in the inserter.
So from that, the init
hook looks to be being called correctly, it’s just that the function register_block_type
doesn’t want to run.
I’m aware the typical convention is to put blocks in a plugin, I’m hoping to keep them in the theme so blocks can’t just be deactivated.
Has anyone any experience of this please?
Thanks,
Ben
Leave an answer