Support

Account

Home Forums Gutenberg wp_insert_post() and add acf blocks to the page

Solving

wp_insert_post() and add acf blocks to the page

  • Hi,

    I would like to create a new page with wp_insert_post() -function and add some of my own acf blocks with content to the page. However I am only able to add default WP blocks to the page, but not acf blocks. Any tips on how to add acf blocks with content to the page?

    Code that works (default WP blocks only):
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘
    <!– wp:heading {“placeholder”:”Post Heading”} –>
    <h2>Here is the title</h2>
    <!– /wp:heading –>

    <!– wp:paragraph {“placeholder”:”Post Paragraph”} –>
    <p>here is some text</p>
    <!– /wp:paragraph –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    What I am trying to do but not able to(add acf blocks):
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘

    <!– acf:my-example-block –>
    <!– /acf:my-example-block –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    Furthermore, I would like to add some content to the blocks like:
    wp_insert_post(
    array(
    ‘post_title’ => ‘Test Post’,
    ‘post_type’ => ‘page’,

    ‘post_content’ => ‘

    <!– acf:my-example-block {“data”:[“content”:”here is the content to the acf field called content”]} –>
    <!– /acf:my-example-block –>’,

    ‘post_status’ => ‘publish’,
    ‘post_author’ => 1,
    )
    );

    Thanks for any help in advance!

  • I was able to add a block WITHOUT content. The issue was that instead of “acf:my-example-block” I needed to write “wp:acf/my-example-block”. However I am not able to put any content to the block generated. The content needs to go to the acf fields of the block. Any tips on this?

    I have tried this without success:
    <!– wp:acf/my-example-block {“data”:{“content”:”here is some content to the acf field”}}–>
    <!– /wp:acf/tt-block-content –>’

  • You have to not only set the “normal” field value but also the underscored version like this:

    
      function myAcfGetBlock(
        string $block,
        array $fieldData
      ) {
    
        $fields = [];
        foreach($fieldData as $fieldKey => $fieldValue) {
          $key = 'block_' . $block . '_' . $fieldKey;
          $fields[$key] = $fieldValue;
          $fields['_'.$key] = 'field_' . md5($key);
        }
    
        $json = [
          'name' => 'acf/' . $block,
          'data' => $fields,
          'mode' => 'edit'
        ];
    
        $fields = json_encode($json);
        $acfJson = <<<ACFJSON
    <!-- wp:acf/$block $fields /-->
    ACFJSON;
    
        return $acfJson;
      }
    

    so you can then do something like

    
    wp_insert_post( [
            'post_type' => 'job',
            'post_status' => 'publish',
            'post_title' => 'My Jobname',
            'post_content' => myAcfGetBlock('title', [
              'subtitle' => 'My subtitle'
            ])
          ] );
    
  • I’m using the code above to create a post with a custom ACF block.

    When I’m adding Block from the panel, then it produces code:

    <!-- wp:acf/pd-text {"name":"acf/pd-text","data":{"field_62d53c8533fbd":"Example title","field_62d53c8533fc5":"Paragraph number one.\r\n\r\nparagraph number two\r\nLine two - paragraph two.\r\n\r\nLine number three.","field_63beb8efe0d5c":"","field_630f632a63aec":"2"},"mode":"edit"} /-->

    When I’m using a function above I have got:

    <!-- wp:acf/pd-text {"name":"acf\/pd-text","data":{"_block_pd-text_columns_in_row":"field_0b8c83bd4bf105c587e3339b7c503995","columns_in_row":"2","_block_pd-text_pd_block_id":"field_370af7474f47f5784d40f48cb2c1a98e","pd_block_id":"","_block_pd-text_title":"field_f33613f99ddfaa923467cbfb44bd66fd","title":"Example title","_block_pd-text_main_text":"field_15dfd2aadd23db2d876eb81842ad1b76","main_text":"Paragraph number one.\n \n paragraph number two\n Line two - paragraph two.\n \n Line number three."},"mode":"edit"} /-->

    My generated code on the front not displaying new lines and also not creating HTML tags.

    As you can see also md5() fields are incorrect, any idea what I’m doing wrong?

    Code to generate JSON:

            $acfOutput = '';
            $acfFields = [];
            $blockName = 'pd-text';
    
            $fieldData = [
                'columns_in_row' => '2',
                'pd_block_id' => '',
                'title' => 'Example title',
                'main_text' => 'Paragraph number one.
              
              paragraph number two
              Line two - paragraph two.
              
              Line number three.'
            ];
    
            if(is_array($fieldData)){
                foreach($fieldData as $fieldKey => $fieldVal) {
                    $key = 'block_' . $blockName . '_' . $fieldKey;
                    $acfFields['_' . $key] = 'field_' . md5($key);
                    $acfFields[$fieldKey] = $fieldVal;
                }
    
                $json = [
                    'name' => 'acf/' . $blockName,
                    'data' => $acfFields,
                    'mode' => 'edit'
                ];
    
                $fields = json_encode($json);
    
                $acfOutput = <<<ACFJSON
                <!-- wp:acf/$blockName $fields /-->
                ACFJSON;
            }
Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.