Skip to content

Instantly share code, notes, and snippets.

@cfinke
Created October 16, 2012 14:44
Show Gist options
  • Save cfinke/3899726 to your computer and use it in GitHub Desktop.
Save cfinke/3899726 to your computer and use it in GitHub Desktop.
Write all of the posts in a WordPress blog as HTML files
<?php
/**
* Drop this file in wp-content/mu-plugins/ (create if it doesn't exist),
* create a writable directory on your server to store the HTML, then call
* http://www.yourblog.com/?html-output-dir=/path/to/dir/you/created.
*/
function write_html_posts() {
remove_filter( 'the_content', 'wptexturize' );
if ( isset( $_GET['html-output-dir'] ) && is_dir( $_GET['html-output-dir'] ) ) {
$_GET['html-output-dir'] = rtrim( $_GET['html-output-dir'], '/' );
$posts = get_posts( array(
'numberposts' => 50000,
'order' => 'ASC',
) );
foreach ( $posts as $post ) {
setup_postdata($post);
$title = get_the_title( $post->ID );
$fh = fopen( $_GET['html-output-dir'] . '/' . $post->ID . '.html', 'w' );
fwrite( $fh, '<!DOCTYPE html><html><head><meta charset="UTF-8" /><title>' . $title . '</title></head><body><h1>' . $title . '</h1><div>' . apply_filters( 'the_content', get_the_content() ) . '</div></body></html>' );
fclose( $fh );
}
wp_die( 'Done.' );
}
}
add_action( 'init', 'write_html_posts' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment