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:
- Is the preview post rendered and routed the same way as the live
- 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?
Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.
Even if you hit just one button for “Preview” it may ends in 2 different “routes”.
The one is something like
The second is something like:
so the first contains
preview query argument, the second contains
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.
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:
- You click “Preview” button
- WordPress stores current post in posts table as revision
- A browser page with post url and
previewquery argument is opened
- WordPress pulls the post object from posts table
- Because of
previewquery argument is present, WordPress
- Verifies current user is logged and has proper capabilities
- Verifies nonce
- 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
- If a valid revision is found, the post object pulled at point
4is 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.