6

I'm trying to find how WordPress is dealing with preview posts before publish.

I'm entering the edit mode of a post that I want to edit, changing some stuff, pressing preview, and getting the preview shown with changes.

My questions are:

  1. Is the preview post rendered and routed the same way as the live one?
  2. If yes, does WordPress save another entity of that post in the database which looks just as the original post but with changes, and that entity is rendered?
6

Even if you hit just one button for "Preview" it may ends in 2 different "routes".

The one is something like

http://example.com/postname/?preview=true

The second is something like:

http://example.com/postname/?preview_id=152&preview_nonce=xxx&preview=true

so the first contains preview query argument, the second contains

  • preview_id
  • preview_nonce
  • preview

query arguments.

preview query argument tells WordPress to allow visualization of non-published posts to users with proper capabilitites.

If you have a post saved as "draft", and you are logged in, you can view it in front-end just like it was published, simply adding ?preview=true to the post url.

The second kind of url are attached to preview button via javascript when a post autosave runs.

An autosave stores the post as it is (so including any change) in posts table, but replacing actual post type with type "revision".

Find more information here.

Autosave runs at regular intervals on backend, but also runs when you click the "Preview" button, in this way in the preview screen you always see last changes.

This is what happen:

  1. You click "Preview" button
  2. WordPress stores current post in posts table as revision
  3. A browser page with post url and preview_id, preview_nonce and preview query argument is opened
  4. WordPress pulls the post object from posts table
  5. Because of preview query argument is present, WordPress
    1. Verifies current user is logged and has proper capabilities
    2. Verifies nonce
    3. If previous checks pass, WordPress pulls last post revision from database, ensuring that revision post name and post author matches post name and post author of post object pulled at point 4
    4. If a valid revision is found, the post object pulled at point 4 is replaced with revision post object

Besides of that, a preview is handled the same way of normal front-end request for a sigular post, but once the post object used to display the page is taken from revision database row, you see the last, even not saved, post changes.

  • Thank you very much! A few questions: in point 2 when you say stores as a revision, that means wp creates a new raw, with same parameters as it would if I published the post, but only the type is revision? If so than every time I click the preview button, is a new instance is created or is it overriding the previous preview instance? – Dima Gimburg Feb 28 '16 at 21:52
  • 1
    Everytime the preview button is clicked, WP adds a row to database, that inherits columns from the row for the "regular" post, but has post type set as "revision" and last changes done in the post edit screen. In the preview screen, the $post instance is then replaced using data from that revision db row. The original post db row is not touched during preview, but it is only updated when you hit "save" button. Note that old revisions db rows are deleted by WP at regular intervals using WP cron. @DimaGimburg – gmazzap Feb 28 '16 at 22:52
  • When I click 'Preview Changes' it alternates between ?preview=true and the longer version with preview_id, etc. So, if I'm on the edit screen, and click 'Preview' 3 times, the first time will be with ?preview=true, the second time will load ?preview_id=152&preview_non..., then the third time it's back to ?preview=true, and so on and so forth, alternating back and forth between URLs with each click. Why is that? Is there any way to make it only ever load with the second, longer URL? – Pete Mar 9 '17 at 4:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.