Save valid JSON to wordpress post content with slashes (escaped html) through wp_insert_post

I have a JSON string that has html properly striped with slashes.

{
  "sections": [
    {
      "section_name": "Objective",
      "data": "<span style="font-weight: bold;">Test</span>",
      "key": "ref"
    }
  ]
}

Now i am trying to insert this json string to worpdres post content through wp_insert_post

Read More
$data = '{"sections":[{"section_name":"Objective","data":"<span style="font-weight: bold;">Test</span>","key":"ref"}]}';
    $new = array(
                'post_title'    => $title,
                'post_content'  => $data,
                'post_author'    => $userid,
                'post_status'   => 'pending',
                'post_type' => 'post'
              );
         $id = wp_insert_post($new );

Now in the inserted post, I see the slashses are automatically removed.

So when i get the post content, it becomes invalid JSON.

However i could directly save the valid JSON through wordpress admin, or to mysql database through phpmyadmin. It works as expected.

How could i save valid JSON to wordpress post content with slashes (escaped html)

Related posts

Leave a Reply

1 comment

  1. data = <<<EOD
    {"sections":[{"section_name":"Objective","data":"<span style="font-weight: bold;">Test</span>","key":"ref"}]}
    EOD;
        $new = array(
                    'post_title'    => $title,
                    'post_content'  => $data,
                    'post_author'    => $userid,
                    'post_status'   => 'pending',
                    'post_type' => 'post'
                  );
             $id = wp_insert_post($new );
    

    This worked.