Created
March 17, 2026 13:57
-
-
Save georgestephanis/8890bdff086f9072dc146c490a6db7e8 to your computer and use it in GitHub Desktop.
If using Kadence Elements to override a template, the WordPress Block Editor's "Preview in new tab π" link renders the saved version of the post, rather than the unsaved changes in the block editor that the user is trying to preview. This code will fix that, but only fix it for the post content -- if there are other unsaved changes such as postmβ¦
This file contains hidden or 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 | |
| /** | |
| * Kadence Elements Preview Fix | |
| * | |
| * This plugin fixes the content for Kadence Elements previews in | |
| * Gutenberg. When previewing, the unsaved content is spliced into | |
| * $GLOBALS['post'], but Kadence runs pulling the saved content | |
| * instead. This plugin ensures that the correct content is | |
| * displayed in the preview. | |
| */ | |
| namespace BigOrangeLab\Kadence\Elements\PreviewFix; | |
| add_filter( 'the_content', __NAMESPACE__ . '\\preview_fix' ); | |
| /** | |
| * Fixes the content for Kadence Elements previews. | |
| * | |
| * When previewing in Gutenberg, the unsaved content is spliced into $GLOBALS['post'], | |
| * but Kadence (incorrectly) runs pulling the saved content instead. | |
| * | |
| * @param string $content The original content. | |
| * @return string The modified content for preview. | |
| */ | |
| function preview_fix( $content ) { | |
| static $guard = false; | |
| if ( is_preview() && doing_action( 'ktp_the_content' ) && ! $guard ) { | |
| $guard = true; | |
| $content = apply_filters( 'the_content', get_the_content( '', false, $GLOBALS['post'] ) ); | |
| $guard = false; | |
| } | |
| return $content; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment