0

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.

Milli
  • 23
  • 4

2 Answers2

0

try using $._data($('form').get(0), "events") instead of jQuery('form').data('events')

If you are using JQuery version over 1.8, then you can't use JQuery.data("events") anymore. Refer to this question.

Community
  • 1
  • 1
GantTheWanderer
  • 1,255
  • 1
  • 11
  • 19
0

I have found the solution to my question:

In jQuery you can use namespaces for events, that this problem does not occur. On the other hand in my example I can simply use the command: "return false" to stop submitting.

Thanks anyway.

Milli
  • 23
  • 4