Created
July 20, 2023 10:40
-
-
Save proweb/c7419ea89e08ab595da8114485dcfdd0 to your computer and use it in GitHub Desktop.
Register multiple block.json in plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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