I have a WordPress site very customized where there are a lot of contributors and lots of posts are published. When you try to publish/update a custom post it displays a popup saying “The changes you made will be lost if you navigate away from this page. Are you sure you want to leave this page?”.
This only happens when the modified/new content of the post is in custom fields corresponding to “Advanced Custom Fields” plugin. This popup is very annoying for the contributors. So I would like to know if is possible to disable this popup or any way to fix that.
Thanks.
Ok, so I was able to track down the cause. It is a JS function in ACF called unload.
All you have to do is add some JS:
Reference:
http://support.advancedcustomfields.com/forums/topic/how-to-disable-js-alert-in-acf_form/
A more general answer in regards to WP’s confirmation dialog:
Within
wp-admin/js/post.js
of WordPress-4.8 on line 481 you can find the mechanism behind the “navigate away” confirmation dialog.The event:
beforeunload.edit-post
is initiated via jQuery’s.on()
function. jQuery gives you the ability to unbind event handlers via the.off()
function.By adding the following to your Javascript, you can disable this confirmation dialog when needed. WordPress itself also uses it when you hit the submit (Publish) button on a post for example.
P.S. WordPress specific questions should be asked at: https://wordpress.stackexchange.com/
Still valid on
wordpress 5.9
So, I was trying to track down how WordPress itself handled this.
Here is what I found out:
There is an
eventListener
on publish or edit button of WordPress.That actually runs the below code:
So, Using the same way you also can disable that.