WordPress: Hook the post update button or form for validation

I want to cancel the post update in some circunstances without the need to reload the entire page.

I’ve tried:

Read More
// publish is the id of the Update button.
$('#publish').click(function(){
    return false;
});

// post is the id of the corresponding form.
$('#post').submit(function(){
     return false;
});

In both codes the form it’s not submitted but the Update button remain disabled and the ajax loader gif is showed.

How I can do this? Thanks in advance.

Related posts

Leave a Reply

4 comments

  1. I think because WordPress already has an event handler on that element it doesn’t matter what it returns, it’s just firing both even handlers. Try this:

    $('#publish').click(function (event) {
        event.stopPropagation();
    });
    
  2. Did you also try:

    jQuery(document).ready(function($) {
        $('#post').submit(function(e) {
            e.preventDefault();
    
            $('.ajax-loading').hide();
            $('#publish').removeAttr("disabled"); 
        });
    });
    
  3. Finally, with the help of those who answered my question (specially to stealthyninja), I managed to bring working what I asked.

    @karevn: This solution doesn’t break the autosave in current WordPress version (tested up to 3.2.1).

    $('#publish').click(function(e) {
        if (!condition) {
    
            // Fix WordPress display to user.
            $('#ajax-loading').hide();
            $('#publish').removeClass('button-primary-disabled');               
    
            return false;
        }     
    });