public
Created

Write all of the posts in a WordPress blog as HTML files

  • Download Gist
html-posts.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
<?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' );

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.