Support

Account

Home Forums Gutenberg Registering Multiple Blocks

Unread

Registering Multiple Blocks

  • Hi there,
    I’m trying to figure out a way to register multiple blocks at a time.

    Here’s how I’m trying to do it:

    add_action( 'acf/init', 'register_call_to_action_block_2' );
    function register_call_to_action_block_2() {
    
    	if ( function_exists( 'acf_register_block_type' ) ) {
    
    		// Register Call to Action block.
    		acf_register_block_type(
    			array(
    				'name'            => 'call-to-action',
    				'title'           => __( 'Call to Actions' ),
    				'description'     => __( 'A custom Call to Action block.' ),
    				'category'        => 'formatting',
    				'icon'            => 'layout',
    				'keywords'        => array( 'call', 'to', 'action' ),
    				'post_types'      => array( 'post', 'page' ),
    				'mode'            => 'preview',
    				'render_template' => plugin_dir_path( __FILE__ ) . '../views/block-call-to-action.php',
    				'enqueue_style'   => plugin_dir_url( __FILE__ ) . '../assets/css/bplogix-blocks.css',
    			),
    			array(
    				'name'            => 'call-out',
    				'title'           => __( 'Call Out' ),
    				'description'     => __( 'A custom Call Out block.' ),
    				'category'        => 'formatting',
    				'icon'            => 'layout',
    				'keywords'        => array( 'call', 'out' ),
    				'post_types'      => array( 'post', 'page' ),
    				'mode'            => 'auto',
    				'render_template' => plugin_dir_path( __FILE__ ) . '../views/block-call-out.php',
    				// 'render_callback'	=> 'call_out_block_render_callback',
    				// 'enqueue_style' 		=> get_template_directory_uri() . '/template-parts/blocks/call-out/call-out.css',
    				// 'enqueue_script' 	=> get_template_directory_uri() . '/template-parts/blocks/call-out/call-out.js',
    				// 'enqueue_assets' 	=> 'call_out_block_enqueue_assets',
    			)
    		);
    
    	}
    
    }
    

    And then the callback

    function my_acf_block_render_callback( $block ) {
    
    	// convert name ("acf/testimonial") into path friendly slug ("testimonial")
    	$slug = str_replace( 'acf/', '', $block['name'] );
    
    	// include a template part from within the "template-parts/block" folder.
    	if ( file_exists( plugin_dir_path( __FILE__ ) . "/views/block-{$slug}.php" ) ) {
    		include plugin_dir_path( __FILE__ ) . "/views/block-{$slug}.php";
    	}
    
    }
    

    I tried doing a foreach loop instead but that didn’t seem to work either. Clearly I’m doing something wrong. I saw another topic here on this but that didn’t answer my question unfortunately.

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.