我在前端形式中使用带有ajax的wordpress,我需要支持处理和上传特色图像。我的问题是关于特色图像。我的HTML是这样的:
<form id="msform" action="#" method="post" enctype="multipart/form-data">
//inputs of various nature
<input type="file" name="main_image" id="main_image" multiple="false" value="" accept=".png, .jpg, .jpeg, .gif"/>
<input type="submit" class="submit" value="Publish"/>
</form>
我通过这个jquery发送数据到php函数(遵循Wordpress方法):
function apfaddpost() {
var formData = $('#msform').serialize();
formData.append('main_image', $('#main_image')[0].files[0]); //here should be the problem
jQuery.ajax({
type: 'POST',
url: apfajax.ajaxurl,
data: formData + '&action=apf_addpost', //here I send data to the php function calling the specific action
processData: false,
contentType: false
success: function(data, textStatus, XMLHttpRequest) {
var id = '#apf-response';
jQuery(id).html('');
jQuery(id).append(data);
resetvalues();
},
error: function(MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
我的函数php类似于
function apf_addpost() {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
$file_handler = 'main_image';
$attach_id = media_handle_upload($file_handler,$pid );
update_post_meta($pid,'_thumbnail_id',$attach_id);
}
重要的是说:标题,描述,标签等所有其他数据都已正确序列化并发送。问题出在图像上。我也试过使用$_FILES[]
处理程序但没有成功,我想我的ajax代码并不那么好。你能帮助我吗?如果您对此问题有任何其他解决方法,请分享!提前谢谢。
[已解决]编辑
感谢下面的答案,我刚刚将我的ajax改为
function apfaddpost() {
var fd = new FormData($('#msform')[0]);
fd.append( "main_image", $('#main_image')[0].files[0]);
fd.append( "action", 'apf_addpost');
//Append here your necessary data
jQuery.ajax({
type: 'POST',
url: apfajax.ajaxurl,
data: fd,
processData: false,
contentType: false,
success: function(data, textStatus, XMLHttpRequest) {
var id = '#apf-response';
jQuery(id).html('');
jQuery(id).append(data);
resetvalues();
},
error: function(MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
我发现FormData()
允许序列化文件,.serialize()
方法没有。众所周知,继续前进很简单。
感谢。
答案 0 :(得分:5)
请尝试:
我修改了你的代码。
Jquery (添加了FormData()并附加)
function apfaddpost() {
var fd = new FormData();
fd.append( "main_image", $('#main_image')[0].files[0]);
fd.append( "action", 'apf_addpost');
//Append here your necessary data
jQuery.ajax({
type: 'POST',
url: apfajax.ajaxurl,
data: fd,
processData: false,
contentType: false
success: function(data, textStatus, XMLHttpRequest) {
var id = '#apf-response';
jQuery(id).html('');
jQuery(id).append(data);
resetvalues();
},
error: function(MLHttpRequest, textStatus, errorThrown) {
alert(errorThrown);
}
});
}
function.php 中的
我添加了文件上传代码
/******FILE UPLOAD*****************/
function upload_user_file( $file = array() ) {
require_once( ABSPATH . 'wp-admin/includes/admin.php' );
$file_return = wp_handle_upload( $file, array('test_form' => false ) );
if( isset( $file_return['error'] ) || isset( $file_return['upload_error_handler'] ) ) {
return false;
} else {
$filename = $file_return['file'];
$attachment = array(
'post_mime_type' => $file_return['type'],
'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $filename ) ),
'post_content' => '',
'post_status' => 'inherit',
'guid' => $file_return['url']
);
$attachment_id = wp_insert_attachment( $attachment, $file_return['url'] );
require_once(ABSPATH . 'wp-admin/includes/image.php');
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $filename );
wp_update_attachment_metadata( $attachment_id, $attachment_data );
if( 0 < intval( $attachment_id ) ) {
return $attachment_id;
}
}
return false;
}
现在修改 function.php
中的功能apf_addpost()
function apf_addpost() {
foreach( $_FILES as $file )
{
if( is_array( $file ) ) {
$attach_id =upload_user_file(); //Call function
update_post_meta($pid,'_thumbnail_id',$attach_id);
}
}
}