I'm writing a small Plugin for WordPress, in which I use jQuery to add a confirm message, when clicking the "Publish" button when editing or adding posts, pages or other post-types, if the user is sure to publish.
I have following jQuery code:
var wp_core_submit_handlers = null;
//Add click handler to the "Publish" button
jQuery('#publish[value="Publish"]').bind('click', function (){
var do_publish = confirm('Are you sure to publish?');
if (do_publish == false){
//Save the handlers (not working)
wpcore_submit_handlers = jQuery('form').data('events');
//Remove the submit events
jQuery('form').submit(function(e){
e.preventDefault();
});
window.setTimeout(function() {
//Restore the submit.
jQuery('form').submit(function(e){
jQuery(this).unbind('submit').submit()
});
//Restore the core sumit handlers, doesn't work
if (wp_core_submit_handlers) {
jQuery.each(wp_core_submit_handlers, function (event, handlers) {
jQuery.each(handlers, function (j, handler) {
jQuery('form').bind(event, handler);
});
});
}
}, 500);
}
});
The problem is, that I have some core handlers bound to the submit event, which I want to restore after canceling the form submit has removed them. E.g. you write text, press on "Publish" cancel it and press on "Save Draft". The message will appear if you want to leave the page. It should use the core handlers instead and no message should apear.
I'm a beginner in jQuery. Please help me for a solution. Thanks.