Skip to content

Instantly share code, notes, and snippets.

@proweb
Created July 20, 2023 10:40
Show Gist options
  • Save proweb/c7419ea89e08ab595da8114485dcfdd0 to your computer and use it in GitHub Desktop.
Save proweb/c7419ea89e08ab595da8114485dcfdd0 to your computer and use it in GitHub Desktop.
Register multiple block.json in plugin
<?php
/**
* Automatically registers block types by scanning the build/blocks folder.
*
* This function searches for JSON files within each subfolder and registers
* them as block types. It is triggered on WordPress 'init' action.
*/
function auto_register_block_types() {
if ( file_exists( __DIR__ . '/build/blocks/' ) ) {
$block_json_files = glob( __DIR__ . '/build/blocks/*/block.json' );
foreach ( $block_json_files as $filename ) {
$block_folder = dirname( $filename );
register_block_type( $block_folder );
}
}
}
add_action( 'init', 'auto_register_block_types' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment