Support

Account

Home Forums Gutenberg Adding ACF blocks to a block template

Solving

Adding ACF blocks to a block template

  • Hiya

    I have been following the code examples on:

    https://github.com/WordPress/gutenberg/blob/master/docs/templates.md

    To be able to add additional blocks to forma block template. Is there any way to integrate an ACF block into these templates?

    My code so far is:

    function my_add_template_to_posts() {
    	$post_type_object = get_post_type_object( 'page' );
    	$post_type_object->template = array(
    		array( 'core/paragraph', array(
    			'placeholder' => 'Add a root-level paragraph',
    		) ),
    		array( 'core/columns', array(), array(
    			array( 'core/column', array(), array(
    				array( 'core/image', array() ),
    			) ),
    			array( 'core/column', array(), array(
    				array( 'core/paragraph', array(
    					'placeholder' => 'Add a inner paragraph'
    				) ),
    			) ),
    		) )
    	);
    	$post_type_object->template_lock = 'all';
    }
    add_action( 'init', 'my_add_template_to_posts' );
    

    But not sure how to reference a pre made ACF block?

    Any help would be grateful

    TIA

    Tom

  • Hi Tom,

    I was expirimenting with the same thing as i couldnt find information about this.

    But for example:

    First You create a custom block via acf_register_block:

    
    acf_register_block(
    array(
    	'name'              => 'blockname',
    	'title'             => __('title'),
    	'description'       => __('description')
    		
    ));
    

    Then you can take the name of the acf_block ‘blockname’ and use it as following in your post type template:

    
    	$post_type_object->template = array(
    		array( 'acf/blockname', array() ),		
    	);
    

    Note: If your blockname contains a space or a underscore, acf renames this to a dash, so ‘block_name’ becomes ‘block-name’.

    Hope this helps!

    • kkoyan

    • February 5, 2020 at 2:07 am

    Taking this a bit further, is there a way to add default values in a template?

    In the wordpress documentation I see that you can add for some core blocks a placeholder.
    https://developer.wordpress.org/block-editor/developers/block-api/block-templates/
    Is there a way to add default content in acf blocks when you define them in the block template?

Viewing 3 posts - 1 through 3 (of 3 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.