Home Forums Gutenberg Programmatically set blocks initial value


Programmatically set blocks initial value

  • Hi all,

    My use case is the following:
    I have custom post type and custom ACF Gutenberg blocks registered with it.
    I want when new post from this posttype is created, the Gutenberg editor to open with some default blocks in it. It is possible via the template option when creating the post type.

    Here is what it looks like:

    function demo_register_book_post_type() {
      $args = array(
          'public' => true,
          'label'  => 'Demo',
          'show_in_rest' => true,
          'template' => array(
              array( 'acf/custom-block-1', array(
                  'align' => 'left',
              ) ),
              array( 'acf/custom-block-2', array(
                  'placeholder' => 'Some placeholder...',
              ) ),
      register_post_type( 'demo', $args );
    add_action( 'init', 'demo_register_book_post_type' );

    My problem is that I could not fill set initial values to these blocks. If I have field ( ex. name ) I want to set default name to the blocks. Doing something like:

    'template' => array(
      array( 'acf/custom-block-1', array(
          'name' => 'Initial name',
      ) ),

    does not work and results with JS error Uncaught TypeError: Cannot read property ‘preview’ of undefined coming from acf-pro-blocks.min.js file.

    Any way to set values of these blocks?

    Any help appreciated.

  • Ok, my bad, I was stupid:

    The options should be wrapped in data array:

    'template' => [
         ['acf/my-block', [
           'data' => [
            'name' => 'default name',

    I will still leave the post here so anyone with similar problem could benefit from it.

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