Perhaps this solution will work:
var flag_ok = false;
$('#publish').on('click', function (e) {
if ( ! flag_ok ) {
e.preventDefault();
var url = shiftajax.ajaxurl;
var shift = $('#post_ID').val();
var data = {
'action': 'wpaesm_check_for_schedule_conflicts_before_publish',
'shift': shift,
};
$.post(url, data, function (response) {
if( response.action == 'go' ) {
// there aren't any scheduling conflicts, so we can publish the post
//$('#hidden_post_status').val('publish');
flag_ok = true;
$('#publish').trigger('click');
} else {
// there are scheduling conflicts, so ask the user if they want to publish
if (confirm(response.message)) {
//$('#hidden_post_status').val('publish');
flag_ok = true;
$('#publish').trigger('click');
} else {
// do nothing
}
}
});
}
});
This is just something quick put together but it illustrates the fact that you can add a conditional before calling the preventDefault
method. Also, you won’t need to mess with any hidden form fields.
Edit The reason it doesn’t work with submit()
is that there is a handler attached to the click
event.