Thank you, up front for taking the time to look into my problem.
I have around 605 posts I extracted from a database of a site that was running on ruby on rails
. I converted the data into JSON
which I had retrieved from the database. Now I am getting
"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>",
Now I wish to enter the data into wp_posts
. I've gotten all the other parameters correct but I can't figure out how I should deal with wp_posts.post_content
to enter the data from the above given 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>";
}
Any help is much appreciated...
P.S: I know database doesn't allow HTML tags due to occurance of a security vulnerability.