import

Here is a simple method that you can use to import news, posts or content from a text file into WordPress. This script assumes the text data file has each post on a separate line with fields separated by a |. This is how most flat file news scripts store data. It also uses the php tidy extension to clean the input before inserting into the database. If you need to import data from a different source, all you need to do is include the wp-load.php script and use wp_insert_post to insert post.

 TRUE,
    'output-xhtml' => TRUE,
'drop-font-tags' => 'yes',
'drop-empty-paras' => 'yes',
'quote-ampersand' => 'yes',
'quote-marks' => 'yes',
'drop-proprietary-attributes' => 'yes',
'logical-emphasis' => 'yes',
'show-body-only' => TRUE,
'wrap' => 0,
);
 
$user_ID = 1;
foreach ($all_news as $news) {
list($date, $author, $title, $short_story, $full_story, $avatar) = explode('|', $news);
 
$tidy = tidy_parse_string($title, $tidy_config, 'ascii');
$tidy->cleanRepair();
$title = $tidy;
unset($tidy);
 
$story = empty($full_story) ? $short_story : $full_story;
$story = str_replace('{nl}', '', $story); // remove new line tag from cute news
 
$tidy = tidy_parse_string($story, $tidy_config, 'ascii');
$tidy->cleanRepair();
$story = $tidy;
unset($tidy);
 
$story = preg_replace('#

s* s*

#im’, ”, $story); // remove empty paragraphs $new_post = array( ‘post_title’ => $title, ‘post_content’ => $story, ‘post_status’ => ‘publish’, ‘post_date’ =>date(‘Y-m-d H:i:s’, $date), ‘post_author’ => $user_ID, ‘post_type’ => ‘post’, ‘post_category’ =>array(0) ); $post_id = wp_insert_post($new_post); echo $post_id . ‘
‘; }

About Author:

Kalpesh Chavada

Kalpesh Chavada is Our Founder & CEO of Akshar Group Technologies. He is serving with 11+ years of experience in different technologies like Magento, WordPress, Laravel, jQuery, Shopify, Branding, and Digital Marketing. He is very good with creative logic and always ready to take on challenges.

Kalpesh Author