AdvancedCustomFields / AdvancedCustomFields/acf
Insert ACF Block with wp_insert_post
Nobody has claimed this yet.
- Dominant language
- PHP
- Stars
- 945
- Forks
- 197
- PR merge metrics
- No merged PRs in 30d
Description
I have an ACF Block called "pd-text".
The block contains the fields: title | main_text | pd_block_id | columns_in_row.
When I add block in the Admin panel I have generated code:
<!-- wp:acf/pd-text {"name":"acf/pd-text","data":{"field_62d53c8533fbd":"Block title","field_62d53c8533fc5":"Paragraph 1 - text text text.\r\n\r\nParagraph 2 - text text text\r\nLine two - \u003cstrong\u003eparagraph\u003c/strong\u003e two.\r\n\r\nParagraph 3 - text \u0022quoted text\u0022 text text.","field_63beb8efe0d5c":"my-block-id","field_630f632a63aec":"1"},"mode":"edit"} /-->
I would like to build a code that will insert ACF Blocks to the post_content with the function wp_insert_post();
The problem that I have is that the generated JSON output is incorrect.
On the front there aren't rendered html tags
and also not adding the new line.
I found the code to use md5() function to generate underscore field key, but the value is different that this what is on the code above, so I suppose that also is wrong. My question is how to find the field ID?
What I'm doing wrong to save the ACF block with the insert function?
Generated ACF Block JSON:
<!-- wp:acf/pd-text-with-bg-image {"name":"acf\/pd-text-with-bg-image","data":{"_block_pd-text-with-bg-image_columns_in_row":"field_b716db7dfe0831e64233e682bc0a8280","columns_in_row":"1","_block_pd-text-with-bg-image_pd_block_id":"field_27f7ac094f1ba4b063cf82d301713de2","pd_block_id":"my-block-id","_block_pd-text-with-bg-image_title":"field_aa88e266bf0bbbee20d20d4d7172c755","title":"Block title","_block_pd-text-with-bg-image_main_text":"field_922e1888941df5b1fc930ab8a9eaab91","main_text":"Paragraph 1 - text text text.\n \n Paragraph 2 - text text text\n Line two - <strong>paragraph<\/strong> two.\n \n Paragraph 3 - text \"quoted text\" text text."},"mode":"edit"} /-->
My code:
$acfOutput = '';
$acfFields = [];
$blockName = 'pd-text';
$fieldData = [
'columns_in_row' => '1',
'pd_block_id' => 'my-block-id',
'title' => 'Block title',
'main_text' => 'Paragraph 1 - text text text.
Paragraph 2 - text text text
Line two - <strong>paragraph</strong> two.
Paragraph 3 - text "quoted text" text text.'
];
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;
}
...
$postData['post_content'] = $acfOutput;
wp_insert_post($postData);
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the wp_insert_post() call and compare its post_content input with the manually generated ACF block JSON. Review the generated field IDs, md5-derived keys, and newline or HTML handling; done means the inserted block produces the expected field values and rendered paragraphs and line breaks.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- php, wordpress
- Domain
- backend, content
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100