wordpresswordpress-rest-api

WordPress REST API to create a post strips some of the content


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.


Solution

  • 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);