Skip to content

Instantly share code, notes, and snippets.

@williamukoh
Created October 12, 2016 19:39
Show Gist options
  • Save williamukoh/13f19fc99440855b143669a81a5bd627 to your computer and use it in GitHub Desktop.
Save williamukoh/13f19fc99440855b143669a81a5bd627 to your computer and use it in GitHub Desktop.
add_action('template_redirect', function() {
if( get_query_var('version_id') )
preview();
});
function preview() {
global $post;
if ( ! is_object($post) )
return $post;
$version_id = (int) get_query_var('version_id');
$preview = wp_get_post_autosave($version_id);
if ( ! is_object($preview) ) {
$preview = get_post($version_id);
if( !is_object($preview)) return $post;
}
$preview = sanitize_post($preview);
$post->post_content = $preview->post_content;
$post->post_title = $preview->post_title;
$post->post_excerpt = $preview->post_excerpt;
// Workaround for `redirect_canonical` logic added in 4.0
// See https://core.trac.wordpress.org/changeset/28874
if ( 'page' === get_option( 'show_on_front' ) && $post->ID == get_option( 'page_on_front' ) ) {
add_filter( 'redirect_canonical', '__return_false' );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment