WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Out of curiosity, I'm implementing AJAX in saving post. I didn't know where to start until I found this Q&A. It looks something I can grab and build on. It works but something on the Wordpress part that's stopping it from publishing the post; it just stays as a Draft. Here's a screenshot of the outputted error in the browser console

enter image description here

Here's my code

function my_post_type_xhr(){
    global $post;
    if(POST_TYPE === $post->post_type){
        $post_url = admin_url('post.php'); #In case we're on post-new.php
        echo "
                //Click handler - you might have to bind this click event another way
                $('#post').bind('submit', function(e){
                    //Post to post.php
                    var postURL = '$post_url';

                    //Collate all post form data
                    var data = $('form#post').serializeArray();

                    //Set a trigger for our save_post action
                    data.push({foo_doing_ajax: true});

                    //The XHR Goodness
                    $.post(postURL, data, function(response){
                        var obj = $.parseJSON(response);
                            alert('Successfully saved post!');
                            alert('Something went wrong. ' + response);

                    return false;

function save_my_post_type($post_id){
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return;

    #If this is your post type
    if(POST_TYPE === $_POST['post_type']){
        //Save any post meta here

        #We conditionally exit so we don't return the full wp-admin load if foo_doing_ajax is true
        if(isset($_POST['foo_doing_ajax']) && $_POST['foo_doing_ajax'] === true){
            header('Content-type: application/json');
            #Send a response
            echo json_encode(array('success' => true));
            #You should keep this conditional to degrade gracefully for no JS

Could anyone please tell me what's going on?

share|improve this question

closed as off-topic by kaiser Feb 18 '14 at 2:37

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

Shouldn't POST_TYPE be the post type you're looking for? You're also not actually adding the functions in anywhere, so it's not going to do anything until you call add_action(...) – Elisabeth Sadler Dec 6 '12 at 16:19
POST_TYPE looks like a constant. So long as that constant is actually defined to be the appropriate post type name, the code should work fine. And the OP doesn't mention an error related that constant. – s_ha_dum Dec 6 '12 at 17:21

Browse other questions tagged or ask your own question.