数据(Html)迁移到帖子

时间:2017-02-09 作者:Sheraz Ahmed

Thank you, up front for taking the time to look into my problem.

我从一个网站的数据库中提取了大约605篇帖子ruby on rails. 我将数据转换为JSON 我从数据库中检索到的。现在我

"contentBody": "<p><a href=\\"http:\\/\\/www.rangelandswa.com.au\\/425\\/what-are-rangelands?\\">What are the Rangelands? <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/383\\/kimberley\\">Kimberley <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/384\\/pilbara\\">Pilbara <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/385\\/gascoyne\\">Gascoyne <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/386\\/murchison\\">Murchison <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/387\\/goldfields\\">Goldfields <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/388\\/nullarbor\\">Nullarbor <\\/a>| <a href=\\"http:\\/\\/www.rangelandswa.com.au\\/389\\/western-desert\\">Western Desert<\\/a><\\/p>\\n<p>Rangelands NRM is divided into seven sub-regions:<\\/p>\\n<div style=\\"width: 590px; text-align: center; margin-right: auto; margin-left: auto;\\">\\n<img width=\\"590\\" height=\\"727\\" id=\\"Image-Maps_5201209052109173\\" alt=\\"\\" src=\\"http:\\/\\/webadmin.communitycreative.com.au\\/uploads\\/rangelands\\/Images\\/Maps\\/Rangelands_transparent_big.gif\\" border=\\"0\\" usemap=\\"#Image-Maps_5201209052109173\\" \\/>\\n<map name=\\"Image-Maps_5201209052109173\\" id=\\"_Image-Maps_5201209052109173\\">\\n<area title=\\"Kimberley\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/383\\/kimberley\\" shape=\\"rect\\" coords=\\"322,47,529,192\\" alt=\\"Kimberley\\" \\/>\\n<area title=\\"Desert Rangelands\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/389\\/desert-rangelands\\" shape=\\"rect\\" coords=\\"322,197,529,521\\" alt=\\"Desert Rangelands\\" \\/>\\n<area title=\\"Nullarbor\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/388\\/nullarbor\\" shape=\\"rect\\" coords=\\"378,517,528,621\\" alt=\\"Nullarbor\\" \\/>\\n<area title=\\"Goldfields\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/387\\/goldfields\\" shape=\\"rect\\" coords=\\"252,551,378,633\\" alt=\\"Goldfields\\" \\/>\\n<area title=\\"Murchison\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/386\\/murchison\\" shape=\\"rect\\" coords=\\"124,421,320,546\\" alt=\\"Murchison\\" \\/>\\n<area title=\\"Gascoyne\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/385\\/gascoyne\\" shape=\\"rect\\" coords=\\"42,338,262,419\\" alt=\\"Gascoyne\\" \\/>\\n<area title=\\"Pilbara\\" href=\\"http:\\/\\/www.rangelandswa.com.au\\/384\\/pilbara\\" shape=\\"rect\\" coords=\\"97,233,290,337\\" alt=\\"Pilbara\\" \\/>\\n<\\/map>\\n<\\/div>",
现在我想把数据输入wp_posts. 我已经把所有其他参数都弄对了,但我不知道该怎么处理wp_posts.post_content 输入上述数据JSON.

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} else {

$count = 1;
$json = json_decode($json, true);
echo "<pre>";
foreach ($json as $elements) {
    foreach ($elements as $element) {
        $post_modified = date("Y-m-d H:i:s"); // todays date Format 2017-02-03 14:37:13
        $post_date = str_replace("Z", "", str_replace("T", " ", $element[\'contentPostDate\'])); //get from file
        $post_content = ($element[\'contentBody\']); //get from file
        $post_title = ($element[\'contentTitle\']); // get from file
        $post_name = str_replace(" ", "-", strtolower($post_title)); //convert title to abc-xyz format
        if ($count == 1) {
            $guid = "http://114.141.197.17/~rangelands/?p=297";
            $count++;
        } else {
            $guid = "http://114.141.197.17/~rangelands/?p=" . ($conn->insert_id + 1); // http://114.141.197.17/~rangelands/?p=lastinsertedID+1
        }

        $sql = "INSERT INTO wp_posts (  `post_author`, `post_date`, `post_date_gmt`,  `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`,
                                        `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`,
                                        `post_parent`,`guid`,`menu_order`,`post_type`,`post_mime_type`, `comment_count`) 
                                        VALUES  (2, $post_date, $post_date,  $post_content,
                                        $post_title, NULL , \'publish\', \'open\', \'open\', NULL , $post_name, NULL , NULL , 
                                        $post_modified, $post_modified, NULL , 0, $guid, 0, \'post\', NULL , 0); ";

        if ($conn->query($sql) === TRUE) {
            echo "Record # $conn->insert_id entered successfully";
        } else {
            echo "Error: " . $sql . "<br>" . $conn->error;
        }

        $conn->close();

    }

}
echo "</pre>";
}
非常感谢您的帮助。。。

P、 S:我知道数据库不允许HTML标记,因为出现了安全漏洞。

1 个回复
最合适的回答,由SO网友:Sheraz Ahmed 整理而成

所以我用wp_insert_post($array) 但在我使用之前wp_insert_post() 我必须移除一个过滤器,如下所示。

remove_filter(\'content_save_pre\', \'wp_filter_post_kses\');
remove_filter(\'content_filtered_save_pre\', \'wp_filter_post_kses\');
//在此处添加代码以插入包含html元素的帖子

$post = array(
\'post_content\' => \'<a href="#" title="text">text</a> some random text\',
);
wp_insert_post($post);
//然后重新添加过滤器,使其不会更改WP的默认行为。

add_filter(\'content_save_pre\', \'wp_filter_post_kses\');
add_filter(\'content_filtered_save_pre\', \'wp_filter_post_kses\');