Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
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? –  Chip Bennett May 23 '12 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? –  moose Dec 31 '13 at 12:27
add comment

4 Answers

up vote 5 down vote accepted

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.

share|improve this answer
    
+1.. does what exactly asked in question. –  amit Aug 13 '12 at 8:46
add comment

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.

share|improve this answer
add comment

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."
?>
share|improve this answer
add comment

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

share|improve this answer
1  
Hi jseliger - welcome to WPSE! Please post comments as comments rather than answer, thanks! –  Stephen Harris Sep 27 '12 at 9:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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