Решение было таким:
var file = dataURLtoBlob(canvas.toDataURL());
var img = blobToFile(file, "image.png");
var data = new FormData();
data.append(0, img, "image.png");
data.append( 'my_file_upload', 1 );
$.ajax({
url : './submit.php',
type : 'POST',
data : data,
cache : false,
dataType : 'json',
processData : false,
contentType : false,
success : function( respond, status, jqXHR ){
if( typeof respond.error === 'undefined' ){
console.log('ОТВЕТ: ' + respond.files)
}
else {
console.log('ОШИБКА: ' + respond.error);
}
},
error: function( jqXHR, status, errorThrown ){
console.log( 'ОШИБКА AJAX запроса: ' + status, jqXHR );
}
});
});
function blobToFile(theBlob, fileName){
theBlob.lastModifiedDate = new Date();
theBlob.name = fileName;
return theBlob;
}
Это php:
if( isset( $_POST['my_file_upload'] ) ){
// указать путь к wp-load.php - он у меня такой
require( dirname(__FILE__) . '/../../../../wp-load.php' );
$wordpress_upload_dir = wp_upload_dir();
$files = $_FILES;
$done_files = array();
foreach( $files as $file ) {
$new_file_path = $wordpress_upload_dir['path'] . '/' . $file['name'];
$new_file_mime = mime_content_type( $file['tmp_name'] );
if( move_uploaded_file( $file['tmp_name'], $new_file_path ) ) {
$upload_id = wp_insert_attachment( array(
'guid' => $new_file_path,
'post_mime_type' => $new_file_mime,
'post_title' => preg_replace( '/\.[^.]+$/', '', $file['name'] ),
'post_content' => '',
'post_status' => 'inherit'
), $new_file_path );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
wp_update_attachment_metadata( $upload_id, wp_generate_attachment_metadata( $upload_id, $new_file_path ) );
$done_files[] = $new_file_path;
}
}
$data = $done_files ? array('files' => $done_files ) : array('error' => 'Ошибка загрузки файлов.');
die( json_encode( $data ) );
}
?>