2026 Executive BenchmarkMid-Market Enterprise Modernization: How leaders across unify Commerce, Odoo ERP & AI in 90 days.
WORDPRESS

How to Import Posts from a Text File into WordPress

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 w...

Akshar Group Technologies
Dec 25, 2020
2 min read
Verified Source

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.

php
<script>
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 . ‘
‘; }
</script>
Share this playbook
LET'S BUILD WHAT'S NEXT

What's Holding Your Business Back?

Tell us where you want to go, what's getting in the way, and we'll help you figure out the technology to get there.

No sales pitch. Confidential strategic advisory.