2

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.

3

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();
}
  • You're amazing, that was the exact function I was looking for. Thank you so much. How you know so much about WordPress? is there any site where I can learn about core features of WordPress. Thank you again. – Aumkar Thakur Aug 22 '16 at 11:29
  • Hi, is there any callback function so I can do something right after it saves the page. – Aumkar Thakur Aug 22 '16 at 13:17
  • The best way to learn is just to dig into the source code :). Finding what you're looking for is the hard part, but from there you can just see how WordPress does it. With regards to callbacks, WP uses the heartbeat API to leverage auto-saves, so you can hook into the event 'heartbeat-tick.autosave' hook. See code.tutsplus.com/tutorials/… for more information on the Heartbeat API. – Stephen Harris Aug 23 '16 at 10:58

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.