Home › Forums › Gutenberg › wp_insert_post() and add acf blocks to the page › Reply To: wp_insert_post() and add acf blocks to the page
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;
}
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.