A temp solution for the is_preview WP issue

In my previous post I wrote about a problem I had with some javascript code that is placed inside the <head> HTML tags of my header.php file. There was a conflict between this small piece of code and the live preview feature of the WP editor, so no editing was possible at all for my posts. Furthermore, this feature could possibly get Google Adsense users into trouble. Michael Hampton has written an explanatory article about this issue.

A first solution was to use the new is_preview() function in order to avoid the execution of certain code snippets or the appearance of ads in the live preview frame. But, this seems not to be enough as this function does not work as expected in all cases. Here I provide another temporary fix, which seems to work.

The following was the first solution:

<?php if ( !is_preview() ) { ?>
<?php } ?>

Using this way to exclude content or code from the live preview works only when editing drafts. If you try to edit an already published public or private post, then the above if clause does not exclude things from the preview. At first, it seems that this function does not return the proper “TRUE” value when editing published posts or it does not return any value. Unfortunately, I cannot say for sure as I am no developer. I have already filed a bug report at the WordPress Trac, where I explain my problem with more detail.

After spending some hours with this, I ended up with the following solution, which seems to work in all cases and can effectively exclude code snippets or page content from the live preview frame. This is not an official fix.

<?php if ( !isset($_SERVER['HTTP_REFERER']) || $_SERVER['REQUEST_URI'] == '/' || substr($_SERVER['HTTP_REFERER'], strlen(get_bloginfo('url')), 36) != '/wp-admin/post.php?action=edit&post=' ) { ?>
<?php } ?>

In this method, instead of the is_preview() WP function, the server variables of the HTTP request are being used in order to determine if the post is presented in the live preview frame, so that the content inside the if clause gets excluded.

I repeat that this is a temporary solution that I use to resolve my issue. You can try it to see if it works for you. You are encouraged to provide your feedback. If you have a better solution, I would be glad to read about it.

A temp solution for the is_preview WP issue by George Notaras is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Copyright © 2006 - Some Rights Reserved

George Notaras avatar

About George Notaras

George Notaras is the editor of the G-Loaded Journal, a technical blog about Free and Open-Source Software. George, among other things, is an enthusiast self-taught GNU/Linux system administrator. He has created this web site to share the IT knowledge and experience he has gained over the years with other people. George primarily uses CentOS and Fedora. He has also developed some open-source software projects in his spare time.