How do you prevent the submit post AJAX event on the WordPress Edit Post page?

I would like to prevent the Publish/Update button from working if the input in my plugin meta box is invalid.

I’ve already looked through WordPress source code, and found this in post.js:

$('#post').on( 'submit', function( event ) {
    if ( ! updateText() ) {
        event.preventDefault();
        ...
    }
} );

updateText is just a closure that returns false if the values in the Publish on fields are invalid. It throws focus back to these fields, and prevents the save from happening.

I added the same function in my meta box’s front-end script. The problem is that my code works, except event.preventDefault(); doesn’t keep the save event from continuing. WordPress writes the updated post to the database and refreshes the page as usual.

Since my meta box works with site options (not post meta), I can’t do the usual ‘Prevent post publish by setting its status to draft’ approach.

tl;dr my question is: Can I use jQuery to prevent the save post AJAX event from firing on the New / Edit Post page? If so, how?

Read more here: How do you prevent the submit post AJAX event on the WordPress Edit Post page?

Leave a Reply

Your email address will not be published. Required fields are marked *