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 = "";
        } else {
            $guid = "" . ($conn->insert_id + 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;



echo "</pre>";

Any help is much appreciated...

P.S: I know database doesn't allow HTML tags due to occurance of a security vulnerability.

  • Just curious - you couldn't use a higher level WP function (e.g. wp_insert_post) and instead had to go to a direct SQL call? – C C Feb 9 '17 at 14:40
  • I am writing a separate script outside WP on my local machine, connecting just the database and working with it. However, if wp has it's own way of doing this, I'll happily write the script in WP. Suggestion ? – Sheraz Ahmed Feb 9 '17 at 14:47
  • nah, I was just wondering. But I do tend to trust the organic WP method more than trying to roll my own DB functions. If I was doing this, I'd write it as a plugin and use wp_insert_post... – C C Feb 9 '17 at 14:48
  • wp_insert_post() passes data through sanitize_post(), which itself handles all necessary sanitization and validation (kses, etc.). but I need the images and links as they are in the wordpress... I will write a plugin for this if I find a solution. – Sheraz Ahmed Feb 9 '17 at 14:56
  • have you considered converting the data to the XML format for importing that WordPress accepts? – David Lee Feb 9 '17 at 16:20

So I solved my problem using the wp_insert_post($array) but before I used wp_insert_post() I had to remove a filter as shown below.

remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');

// Add code to insert post(s) with html elements here

$post = array(
'post_content' => '<a href="#" title="text">text</a> some random text',

// Then add the filters back so it doesn't change the default behavior of WP.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.