Support

Account

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…