Php Wordpress前端通过文件上传创建帖子(自定义帖子类型)

Php Wordpress前端通过文件上传创建帖子(自定义帖子类型),php,jquery,ajax,wordpress,Php,Jquery,Ajax,Wordpress,我正在制作一个简单的插件,允许用户向自定义帖子类型添加内容。前端表单包括几个文本字段和一个文件上载字段。 我正在使用ajax提交表单,它运行良好。然而,我正试图使文件上传字段也能正常工作。文件上传后,其值(url)将添加为自定义字段 我对ajax不太熟悉,这可能就是我迄今为止尝试失败的原因。以下是我目前使用的代码,不包括文件上传内容: JS: (function($) { $('.btn').on('click', function(e) { $('.btn').html('<

我正在制作一个简单的插件,允许用户向自定义帖子类型添加内容。前端表单包括几个文本字段和一个文件上载字段。 我正在使用ajax提交表单,它运行良好。然而,我正试图使文件上传字段也能正常工作。文件上传后,其值(url)将添加为自定义字段

我对ajax不太熟悉,这可能就是我迄今为止尝试失败的原因。以下是我目前使用的代码,不包括文件上传内容:

JS:

(function($) {

  $('.btn').on('click', function(e) {
    $('.btn').html('<i class="fa fa-spinner fa-pulse"></i>')
    e.preventDefault();
    var fname = $('#first-name').val();
    var lname = $('#last-name').val();
    var email = $('#email').val();
    var address = $('#address').val();
    var phone = $('#phone').val();
    var security = $('#security').val();
    //var file = $('#entryfile').files[0];

    // ajax the results!
    $.ajax({
      url: ajax.ajax_url,
      type: "POST",
      dataType : "json",
      contentType: false,
      processData: false,
      data: {
        action: 'entry_add',
        fname:fname,
        lname:lname,
        email:email,
        address:address,
        phone:phone,
        nonce: security,
        file:file
      },
      success: function( response ) {
        console.log( response );
        $('.contest-entry').hide('500', function() {
          $('.message > h2').html( response );
          $('.message').show('fast');
        });
      }
    });

  });

})( jQuery );
function ajax_entry() {
  check_ajax_referer( 'add-entry-nonce', 'nonce' );

  $email = urldecode($_POST['email']);
  $fname = wp_strip_all_tags($_POST['fname']);
  $lname = wp_strip_all_tags($_POST['lname']);
  $addr  = wp_strip_all_tags($_POST['address']);
  $phone = wp_strip_all_tags($_POST['phone']);

  $success = "<i class='fa fa-check'></i> Thanks for joining! You will be reditected to step 2 in a few seconds";
  $error = "<i class='fa fa-exclamation-triangle'></i> Looks like something went wrong! Try again maybe?";

  if( !empty($email) && !empty($fname) && !empty($lname) && !empty($addr) ) {
    $postData = array(
      'post_title'  => $fname . ' ' . $lname,
      'post_type'   => 'contestants',
      'post_status' => 'draft'
    );
    $post = wp_insert_post( $postData );
    add_post_meta($post, "field_entry_unique", "WTH-".$post);
    add_post_meta($post, "field_first_name", $fname);
    add_post_meta($post, "field_last_name", $lname);
    add_post_meta($post, "field_email", $email);
    add_post_meta($post, "field_address", $addr);
    add_post_meta($post, "field_status", "pending");
    add_post_meta($post, "field_phone", $phone);

    if( $post != -1 ) {
      //echo json_encode($success);
      echo json_encode($file);
    } else {
      echo json_encode($error);
    }
  } else {
    echo json_encode($error);
  }
  exit;
}

你能解释一下失败的地方吗?当它失败时,你做了什么来调试它?目前,我只是想弄清楚如何上传文件。上面的代码工作正常,但不处理上传。我需要帮助来弄清楚怎么做。。我尝试读取$_FILES数组,但它总是空的…可能是或的副本,因为您应该能够在HTML5中执行它
 var formData = new FormData();
      formData.append("action", "upload-attachment");
      var fileInputElement = document.getElementById("entryfile");
      formData.append("async-upload", fileInputElement.files[0]);
      formData.append("name", fileInputElement.files[0].name);
      formData.append("_wpnonce", upload_nonce);
      var xhr = new XMLHttpRequest();
      xhr.onreadystatechange=function(){
        if (xhr.readyState==4 && xhr.status==200){
          console.log(xhr.responseText);             
        }
      }
      xhr.open("POST","wp-admin/async-upload.php",true);
      xhr.send(formData);