我需要从用户那里获取图像输入,并使用AJAX将其保存到WordPress网站数据库中,而无需任何插件。我该如何解决问题?
//function.php
add_action('wp_head', 'myplugin_ajaxurl');
function myplugin_ajaxurl() {
echo '<script type="text/javascript">
var ajaxurl = "' . admin_url('admin-ajax.php') . '";
</script>';
}
//ajax code
jQuery(document).ready(function(){
jQuery('#fupForm').on('submit', function(e){
e.preventDefault();
// var file = jQuery("file#file").val();
var fd = new FormData(this);
var files = jQuery('#image')[0].files[0];
fd.append('file',files);
jQuery.ajax({
url: ajaxurl,
type: "POST",
data : {
files,
action:'my_ajax_callback_function',
},
contentType: false,
processData: false,
beforeSend: function(){
jQuery('#fupForm').css("opacity",".5");
},
success: function(response){
alert(response);
jQuery('#fupForm').css("opacity","");
}
});
});
});
答案 0 :(得分:0)
有两篇文章确实是您的疑问,看看是否能为您解决任何问题,只是警告我会尽力帮助您
Wordpress and AJAX - Upload image as featured 要么 https://wordpress.stackexchange.com/questions/198781/wordpress-ajax-file-upload-frontend