0

I have a custom post type called "shift" (as in shifts worked by employees). When shifts are published, I run an Ajax function to check that this shift does not have a scheduling conflict. If there is a scheduling conflict, a dialog box pops up alerting the user of the conflict and asking if they want to continue. If they choose "cancel", the post isn't published. If they choose "continue", the post is published.

Here's the problem: if they choose "continue", the post gets saved as a draft instead of being published. How can I make the post be published when they click the "publish" button?

Here is the JS (simplified a bit so you can see the important parts):

$('#publish').on('click', function (e) {

    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'); 
            $('#post').submit();
        } else {
            // there are scheduling conflicts, so ask the user if they want to publish
            if (confirm(response.message)) {
                $('#hidden_post_status').val('publish');
                $('#post').submit();
            } else {
                // do nothing
            }
        }
    });

});

Note the line $('#hidden_post_status').val('publish'); - that does change the value of the hidden field with an ID of "hidden_post_status", but that doesn't have an effect. The post is still saved as a draft.

  • Share your server side code. Note that if you are using wp_insert_post() default post status is draft. – Sisir Jul 9 '15 at 17:47
  • Sisir - the server side code doesn't do anything related to publishing. I am not using wp_insert_post() or any other functions to create the post - I am letting the "Publish" button create the post. All the server side code does is compare meta fields to existing posts, and if there is a conflict, it returns "stop" and if there is not a conflict, it returns "go." The $('#post').submit(); in the JS is what publishes the post. – Morgan Kay Jul 9 '15 at 17:52
2

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.

  • Brilliant! This did the trick. Thank you so much! – Morgan Kay Jul 9 '15 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.