Home Forums Gutenberg Adding ACF blocks to a block template


Adding ACF blocks to a block template

  • Hiya

    I have been following the code examples on:

    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



  • 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:

    	'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.
    Is there a way to add default content in acf blocks when you define them in the block template?

  • I second kkoyan’s question. Adding Gutenberg template blocks where the ACF block contents are preselected would be appreciated.

  • @jagenda +1

    • jonk

    • May 11, 2020 at 11:02 pm


    I was expecting something like this to work, but computer says no …

    array( ‘acf/blockname’, array(
    ‘header’ => __( ‘You may also like’, ‘td’ ),
    ) ),

    where header is the name of the field.

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