• I’m using ajax to pass a form’s variables to a php processing file from within a fancybox div.

    Everything works, no errors, except the post never makes it to the database.

    I have a form which is populated within a fancybox div:

    <form id ="promo" name="promo" onsubmit="return submitForm();">
    ......form fields
    </form>

    I have an ajax function in header.php which calls the php script:

    function submitForm() {
        $.ajax({type:'POST', url: '/wp-content/themes/buildcom/submit-form.php', data:$('#promo').serialize(), success: function(response) {
            $('#promo').find('.form_result').html(response);
        }});
        return false;
    }

    Finally I have the submit-form.php file which should insert the post, and returns true, but never creates a post no matter the parameters (post, page or custom post type):

    $my_post = array(
      'post_title'    => 'tesssssssssssst',
      'post_content'  => 'test',
      'excerpt'       => 'excerpt',
      'post_status'   => 'publish',
      'post_type'     => 'post'
    );
    echo '<script>console.log("'.var_dump($my_post).'")</script>';
    
    // Insert the post into the database
    wp_insert_post($my_post, $wp_error);
    $wp_error = wp_insert_post( $my_post, true);
                                  echo '<pre>';
                                  print_r ($wp_error);
                                  echo '</pre>';

    I get no error, it returns a post id, and if I dump the $my_post variable it returns all the fields without problem:

    array(5) { ["post_title"]=> string(15) "tesssssssssssst" ["post_content"]=> string(4) "test" ["excerpt"]=> string(7) "excerpt" ["post_status"]=> string(7) "publish" ["post_type"]=> string(4) "post" }
    94
    its set

    I’m stumped… someone please tell me what I’m missing.

Viewing 1 replies (of 1 total)
  • Thread Starter spiderwisp

    (@spiderwisp)

    Anyone? It seems the fancybox is erasing the POST variables and therefore preventing the post from being created, but that is not the case as I can clearly spit the variables out after using wp_insert_post.

    For now I have resorted to using iframes with fancybox, which I don’t like.

Viewing 1 replies (of 1 total)

The topic ‘wp_insert_post fails with no error’ is closed to new replies.