0

I am using https://developer.wordpress.org/rest-api/reference/posts/#create-a-post:

$my_post = 
"<!-- wp:kadence/rowlayout {'uniqueID':'5331_605d8b-3f','columns':1,'colLayout':'equal','minHeight':380,'bgImg':'http://wordlets.si/media/4-banner8.jpg','bgImgPosition':'48% 42%','align':'full','minHeightMobile':180,'mobilePadding':[25,'','',''],'kbVersion':2} --> 
 <!-- wp:kadence/column {'borderWidth':['','','',''],'uniqueID':'5331_0edd64-7f','kbVersion':2,'className':'inner-column-1'} --> 
 <div class='wp-block-kadence-column kadence-column5331_0edd64-7f inner-column-1'>
     <div class='kt-inside-inner-col'></div>
 </div>";

$data = [
'title'   => 'My Post Title',
'content' => $my_post,
'status'  => 'draft',
];

$response = Http::withBasicAuth($username, $password)
    ->post('https://mywp.xyz/wp-json/wp/v2/posts', $data);

But when I open the post on WP, I see this code only:

<!-- wp:kadence/rowlayout -->
<!-- wp:kadence/column -->
<div class='wp-block-kadence-column kadence-column5331_0edd64-7f inner-column-1'>
  <div class='kt-inside-inner-col'></div>
</div>

What is removing some of the content, and how to prevent it? I don't want WP to manipulate my input - as a dev I'll make sure it's correct. Also, I am using Kadence plugin for posts, if that changes anything.

2
  • Just for clarity, if you create a post through the Wordpress UI with the same content, does it work as expected? Also, can you check in the DB to see what the raw value stored looks like? Commented Dec 17, 2024 at 4:59
  • @Phil yes, if I create the post through WP UI, then the content stays the same. I sadly do not have access to DB :( Commented Dec 17, 2024 at 5:18

1 Answer 1

1

I actually found the answer, just had to replace ' with " in the content. Weird, but works:

// replaced ' with ", and put '' around whole string (previously "")
$my_post = 
'<!-- wp:kadence/rowlayout {"uniqueID":"5331_605d8b-3f","columns":1,"colLayout":"equal","minHeight":380,"bgImg":"http://wordlets.si/media/4-banner8.jpg","bgImgPosition":"48% 42%","align":"full","minHeightMobile":180,"mobilePadding":[25,"","",""],"kbVersion":2} --> 
 <!-- wp:kadence/column {"borderWidth":["","","",""],"uniqueID":"5331_0edd64-7f","kbVersion":2,"className":"inner-column-1"} --> 
 <div class="wp-block-kadence-column kadence-column5331_0edd64-7f inner-column-1">
     <div class="kt-inside-inner-col"></div>
 </div>';

$data = [
'title'   => 'My Post Title',
'content' => $my_post,
'status'  => 'draft',
];

$response = Http::withBasicAuth($username, $password)
    ->post('https://mywp.xyz/wp-json/wp/v2/posts', $data);
Sign up to request clarification or add additional context in comments.

1 Comment

Must have to be valid JSON

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.