Created
October 16, 2012 14:44
-
-
Save cfinke/3899726 to your computer and use it in GitHub Desktop.
Write all of the posts in a WordPress blog as HTML files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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