How to add in a Post Preview Option
-
Okay, got it working.
There are some interesting hacks to get this to work, so holler if you need more than the sketchy details here.
First thing first, this is for WP 1.02, not the nightly.
Second, you’ll need to make a copy of the file wp-blog-header.php, located in your root directory. I called mine ‘wp-blog-draft-header.php’.
Once copied, edit the file and just change every instance of ‘publish’ to ‘draft’. Look for the words, ‘No point in doing all this work if we didn’t….’ and delete everything after that point (but not the closing PHP tag).
(You can get a copy of mine at https://weblog.burningbird.net/wp-blog-draft-header.tgz)
I had created a separate individual archive file by copying index.php into a file called individual.php, and then removing most of the stuff I don’t want with the individual archive page — such as the RSS links and monthly archive links and that sort of thing.
To create a Preview page, I copied this individual page to a new page called draftpreview.php. I then edited the header file to use this new ‘draft mode’ header file, not the publish header file.
(Note that I can probably clean a lot of this file up, but I don’t care — it works, it doesn’t hurt anything, that’s all I want.)
The next part is where you have to modify an existing file. MAKE SURE YOU BACK IT UP FIRST!
In the wp-admin directory, edit the edit-form-advanced.php file, modigying the followin section:
<?php echo $saveasdraft; ?> <input type="submit" name="submit" value="Save" style="font-weight: bold;" tabindex="6" />
<input name="publish" type="submit" id="publish" tabindex="10" value="Publish" />
<input name="referredby" type="hidden" id="referredby" value="<?php echo $HTTP_SERVER_VARS['HTTP_REFERER']; ?>" />
To:
<?php echo $saveasdraft; ?> <input type="submit" name="submit" value="Save" style="font-weight: bold;" tabindex="6" />
<input name="publish" type="submit" id="publish" tabindex="10" value="Publish" />
ID' target='_preview'>Preview
<input name="referredby" type="hidden" id="referredby" value="<?php echo $HTTP_SERVER_VARS['HTTP_REFERER']; ?>" />
The part that’s added is the hypertext link to the preview page (make sure the preview page is in the root directory, not the wp-admin.php — there is already a preview.php page in wp-admin).
That’s it. Works a charm. I’m in the process of trying to formalize this into something for the documentation wiki, including how to make the individual archive page, but I wanted to just post this quickly so that people don’t have to wait.
Now, if you’re concerned about people seeing this page (and if you don’t link to it, they won’t see it), you put this into a separate directory, or even into wp-admin, but you’ll have to modify the require directive in the top of the file to
require(‘wp-admin/wp-blog-draft-header.php’);
And you’ll also have to adjust any relative links for stylesheets, images, etc.
Update
You have to save the draft, first, and then edit it to see the Preview button. You can leave the page open as you edit, just click Save and Continue to update the content, and then click Preview again to see the post in process.
Second update
Nice feature on this is that once you publish the post, it can no longer be accessed by the Preview page, which is for drafts only. This way if a snoop finds the page, the only thing they can do is pull up your drafts.
If you want to preview a published page, you’ll have to revert it back to draft mode.
- The topic ‘How to add in a Post Preview Option’ is closed to new replies.