Created
October 12, 2016 19:39
-
-
Save williamukoh/13f19fc99440855b143669a81a5bd627 to your computer and use it in GitHub Desktop.
Temporaru fix for -- https://github.com/bu-ist/bu-versions/issues/19
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
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