Php 使用wp-restapi上传特色图片

Php 使用wp-restapi上传特色图片,php,wordpress,file-upload,media,wordpress-rest-api,Php,Wordpress,File Upload,Media,Wordpress Rest Api,我正在尝试从我使用wp rest api创建的前端表单上传特色图像,但它不起作用。。这是表格的代码 <form action="" enctype="multipart/form-data" id="form" method="post" name="form"> <div id="upload"> <input id="upload_i

我正在尝试从我使用wp rest api创建的前端表单上传特色图像,但它不起作用。。这是表格的代码

<form action="" enctype="multipart/form-data" id="form" method="post" name="form">
<div id="upload">
<input id="upload_img" name="upload_img" type="file">
</div>
<input id="submit" name="submit" type="submit" value="Upload">
</form>
最后是API的代码:

add_action( 'rest_api_init', function () {
  register_rest_route( 'w1/v1', '/test/', array(
        'methods' => 'POST',
        'callback' => 'insertProduct'
        
      ) );
});

function insertProduct( $data ) { 
$title = $data->get_param('product_name') ; 
  $content = $data->get_param('description') ;
  $getImageFile = $data->get_param('img') ; 

 $product_post = array(
            'post_title'=>$title, 
            'post_type'=>'products', 
            'post_content'=>$content,
            'post_status'=>  'publish'
          );
$the_post = wp_insert_post( $product_post );
$uploaddir = wp_upload_dir();
$file = $getImageFile['name'];
if ( wp_mkdir_p( $uploaddir['path'] ) ) {
  $uploadfile = $uploaddir['path'] . '/' . basename( $file );
}
else {
  $uploadfile = $uploaddir['basedir'] . '/' . basename( $file );
}


move_uploaded_file( $getImageFile['tmp_name'] , $uploadfile );
$filename = basename( $uploadfile );


$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
  'post_mime_type' => $wp_filetype['type'],
  'post_title' => sanitize_file_name( $filename ),
  'post_content' => '',
  'post_status' => 'inherit'
);
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
require_once (ABSPATH . 'wp-includes/pluggable.php');

$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile );
wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $the_post, $attach_id );
}
它成功插入了帖子,但没有添加特色图片,有什么帮助吗

add_action( 'rest_api_init', function () {
  register_rest_route( 'w1/v1', '/test/', array(
        'methods' => 'POST',
        'callback' => 'insertProduct'
        
      ) );
});

function insertProduct( $data ) { 
$title = $data->get_param('product_name') ; 
  $content = $data->get_param('description') ;
  $getImageFile = $data->get_param('img') ; 

 $product_post = array(
            'post_title'=>$title, 
            'post_type'=>'products', 
            'post_content'=>$content,
            'post_status'=>  'publish'
          );
$the_post = wp_insert_post( $product_post );
$uploaddir = wp_upload_dir();
$file = $getImageFile['name'];
if ( wp_mkdir_p( $uploaddir['path'] ) ) {
  $uploadfile = $uploaddir['path'] . '/' . basename( $file );
}
else {
  $uploadfile = $uploaddir['basedir'] . '/' . basename( $file );
}


move_uploaded_file( $getImageFile['tmp_name'] , $uploadfile );
$filename = basename( $uploadfile );


$wp_filetype = wp_check_filetype(basename($filename), null );

$attachment = array(
  'post_mime_type' => $wp_filetype['type'],
  'post_title' => sanitize_file_name( $filename ),
  'post_content' => '',
  'post_status' => 'inherit'
);
include_once ABSPATH . 'wp-admin/includes/media.php';
include_once ABSPATH . 'wp-admin/includes/file.php';
include_once ABSPATH . 'wp-admin/includes/image.php';
require_once (ABSPATH . 'wp-includes/pluggable.php');

$attach_id = wp_insert_attachment( $attachment, $uploadfile );
$attach_data = wp_generate_attachment_metadata( $attach_id, $uploadfile );
wp_update_attachment_metadata( $attach_id, $attach_data );
$res2= set_post_thumbnail( $the_post, $attach_id );
}