How to show updated edit on preview URL without clicking preview button

http://localhost/demosite/?page_id=19&preview=true

^ This is the preview URL for a page with id 19, now whenever I made some changes to it’s page content and refresh above URL it don’t show the latest changes I made unless I click preview button, I think preview button trigger some kind of function which save page and then above URL shows the latest edit, I want to know can I call that function which preview button calls when clicked.

1 Answer
1

Yes, though it’s not very well documented and it’s not clear if it forms part of the public API.

When you click preview, WordPress saves a draft and opens the preview URL which shows the latest saved draft.

You can see this by making a change, and waiting for WordPress’ auto-save to kick in. If you do that, and refresh the preview URL it will show the latest draft without you having to click preview or save draft.

So while you can never see a ‘live preview’ in a separate window. You could, if you wanted, prompt WordPress to save a draft when (for example), the text editor loses focus or the window loses focus. You will probably want to limit the number of revisions you keep, however.

The autosave functionality can be found here: https://github.com/WordPress/WordPress/blob/c8d203e1a33540cb59139060e563728478a6b9e5/wp-includes/js/autosave.js

The function you want to call is:

if ( wp.autosave.server ) {
    wp.autosave.server.triggerSave();
}

Leave a Comment