Home › Forums › Gutenberg › Moving register_block_type out of functions.php › Reply To: Moving register_block_type out of functions.php
Hey @samsmyth — I like to keep functions file clean too. Therefore, either from your functions file or a loader file elsewhere, you can use a foreach (changed to your directory structure idea)
— basically you need to load the JSON file for each block.
add_action( 'init', 'cpar_acf_blocks' );
function cpar_acf_blocks() {
foreach ( glob( get_stylesheet_directory() . '/blocks/*/' ) as $path ) {
register_block_type( $path . 'block.json' );
}
}
This will then dynamically check each folder within the blocks directory you made and look for its corresponding block.json file. I like this approach where I can then just add new blocks in and not have to add more code to load each one I add.
Hope this helps…
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.