Skip to content

Instantly share code, notes, and snippets.

@radmen
Created January 2, 2013 21:56
Show Gist options
  • Save radmen/4438549 to your computer and use it in GitHub Desktop.
Save radmen/4438549 to your computer and use it in GitHub Desktop.
Wordpress lazy static site generator
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /static/$1 [L,QSA]
RewriteCond %{DOCUMENT_ROOT}/static/%{REQUEST_URI}/index.html -f
RewriteRule ^(.*)$ /static/$1/index.html [L,QSA]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

I've needed to dump my Wordpress blog to static sites (it's going to be an archive).
There are some methods to do it - probably the best one is here: Make WordPress static.
I wanted to do it other way - instead of wget'ing site static sites will be generated lazily.

<?php
/**
* Front to the WordPress application. This file doesn't do anything, but loads
* wp-blog-header.php which does and tells WordPress to load the theme.
*
* @package WordPress
*/
/**
* Tells WordPress to load the WordPress theme and output it.
*
* @var bool
*/
define('WP_USE_THEMES', true);
ob_start();
/** Loads the WordPress Environment and Template */
require('./wp-blog-header.php');
$html = ob_get_clean();
if(!is_user_logged_in()) {
$dir = 'static/'.ltrim($_SERVER['REQUEST_URI'], '/');
$ext = pathinfo($dir, PATHINFO_EXTENSION);
if(false === empty($ext)) {
$path = $dir;
$chk_dir = dirname($dir);
}
else {
$path = $dir.'index.html';
$chk_dir = $dir;
}
if(false === is_dir($chk_dir)) {
mkdir($chk_dir, 0777, true);
}
if(false === file_exists($path)) {
file_put_contents($path, $html);
}
}
echo $html;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment