7

I wish to export all of my posts as individual plain text files. So, the format may be something like:

title.txt

 

Title: title
Pub date: date
Category: cat

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et

Is this possible? Is there a plugin or workaround to do this?

Thanks.

2
  • Why not just work with the default XML export? One could conceivably write a script that breaks the XML down into individual posts (though such a solution would be off-topic for WPSE), which might be easier than writing a Plugin/custom exporter? May 23, 2012 at 19:58
  • @samiles: It looks very much as if you wanted to switch to a static blog generator like Jekyll. I suppose you already know this article? Dec 31, 2013 at 12:27

4 Answers 4

6

Try this (you may need to bootstrap WP by loading wp-load.php, depending on where you put this code).

$args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    //'posts_per_page' => -1 //uncomment this to get all posts
);

$query = new WP_Query($args);
while ( $query->have_posts() ) : $query->the_post();
    $f = fopen(get_the_title() . '.txt', 'w');
    $content = 'Title: ' . get_the_title() . PHP_EOL;
    $content .= 'Pub date: ' . get_the_date() . PHP_EOL;
    $content .= 'Category: ';
    foreach (get_the_category() as $cat) {
        $content .= $cat->cat_name . ', ';
    }       
    $content .= PHP_EOL . PHP_EOL;
    $content .= get_the_content();

    fwrite($f, $content);
    fclose($f);
endwhile;

Keep in mind, if two posts have the same title, you'll only get one text file.

1
  • Yall remember to check if the script has write permission for the directory you choose :-)
    – PJ Brunet
    May 18, 2023 at 16:39
3

A cursory search doesn't turn up any plugins that do this ... but you could use the built-in exporter as an example for building out your own plugin. It's located in /wp-admin/includes/export.php.

Essentially, it's a PHP page that queries the database to get all of your posts, then dumps the content into a pre-build XML template that can be imported later.

0

It looks very much as if you wanted to switch to a static blog generator like Jekyll. The following answer is based on pdb's answer and should be ready-to-use. My answer can deal with:

  • multiple post with the same title
  • German umlauts

Here it is:

<?
require_once('wp-load.php');

function getFeaturedImage($postid) {
    $featured_image = "";
    $array = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
    if (count($array)>1) {
        $featured_image = $array[0];
        $featured_image = str_replace('http://martin-thoma.com/wp-content/uploads/', '', $featured_image);
    }
    return $featured_image;
}

$args = array(
    'post_type' => 'post',
    'post_status' => 'draft',
    'posts_per_page' => -1 //uncomment this to get all posts
);

$query = new WP_Query($args);
$i=0;
$titles = array();
while ( $query->have_posts() ) : $query->the_post();
    $i++;
    $name = $post->post_name;
    if ($name == "") {
        $name = strtolower(get_the_title());
        $replacements = array(
            ' ' => '-',
            '#' => 'nr',
            '/' => 'or',
            'ö' => 'o',
            'ü' => 'u',
            'ä' => 'a',
            'ß' => 'ss'
        );
        $name = str_replace(array_keys($replacements), $replacements, $name);
    }

    if (array_key_exists($name, $titles)) {
        $name = $post->post_name."-".$post->ID;
    } else {
        $titles[$name] = true;
    }
    echo $name."<br/>";

    $f = fopen("export/".$name. '.md', 'w');
    $content = "---".PHP_EOL;
    $content.= "layout: post".PHP_EOL;
    $content.= "title: ".get_the_title().PHP_EOL;
    $content.= "author: ".get_the_author().PHP_EOL;
    $content.= "date: ".get_the_date('Y-m-d h:i:s').PHP_EOL;
    $content.= "categories: ".PHP_EOL;
    $cats = get_the_category();
    if ($cats) {
        foreach($cats as $category) {
            $content .= '- '.$category->name.PHP_EOL; 
        }
    }
    $content .= 'tags: ';
    $posttags = get_the_tags();

    if ($posttags) {
        $content .= PHP_EOL;
        foreach($posttags as $tag) {
            $content.= "- ".$tag->name.PHP_EOL; 
        }
    } else {
        $content .= "[]".PHP_EOL;
    }
    echo wp_get_attachment_image_src(get_the_post_thumbnail($post->ID));
    $content .= 'featured_image: '.getFeaturedImage($postid).PHP_EOL;
    $content .= '---'.PHP_EOL;    
    $content .= get_the_content();
    fwrite($f, $content);
    fclose($f);
endwhile;
echo $i." posts exported."
?>
-3

"Why not just work with the default XML export"

I don't have an answer, but the OP might want to do this so he or she can import the posts into Devonthink Pro or a similar program.

2
  • 1
    Hi jseliger - welcome to WPSE! Please post comments as comments rather than answer, thanks! Sep 27, 2012 at 9:42
  • The URL is 404.
    – PJ Brunet
    May 18, 2023 at 16:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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