我有一个JSON字符串,其中的html正确地带有斜杠。

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


现在,我试图通过wp_insert_post将这个json字符串插入worpdres发布内容

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


现在在插入的帖子中,我看到斜杠已自动删除。

因此,当我获取帖子内容时,它将变为无效的JSON。

但是我可以通过wordpress admin直接保存有效的JSON,或者通过phpmyadmin保存到mysql数据库。它按预期工作。

我怎样才能将有效的JSON保存到带有斜杠的wordpress帖子内容中(转义的html)

最佳答案

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


这工作了。

09-30 15:11
查看更多