I come from Object Oriented PHP and Laravel, and now I'm building (my first) plugin for wordpress. The plugin it's already working with a basic upload function that allow the user to upload multiple files (photos) from the disk, and create a wp-post (kind: attachment) from them, attaching some other infos:
$files = $_FILES["my_file_upload"];
foreach ($files['name'] as $key => $value) {
if ($files['name'][$key]) {
$file = array(
'name' => $files['name'][$key],
'type' => $files['type'][$key],
'tmp_name' => $files['tmp_name'][$key],
'error' => $files['error'][$key],
'size' => $files['size'][$key]
);
$_FILES = array("upload_file" => $file);
$attachment_id = media_handle_upload("upload_file", 0);
if (is_wp_error($attachment_id)) {
// There was an error uploading the image.
echo "Error adding file";
} else {
// The image was uploaded successfully!
//Take the album id from the URL
if ($_GET['album_id'] != ''){
wp_update_post(
array(
'ID' => $attachment_id,
'post_parent' => $_GET['album_id']
)
);
echo 'id album'.$_GET['album_id'];
}
Everythings works fine. The problem is that if the user upload to many files of course there is the possibility of running out of memory.
What I'd like to do:
I want to make a script (I'm good in vue, but I can do it also in jQuery or in pure js) that after the user has selected all the files that he wants to upload, makes an array of this files, and then through an AJAX request, it add one file per time (per each request), of course showing to the user adding file 1 of x--- and so on. I already worked (last week) with AJAX in Wp, at the begin it was a bit painful (I didn't know at all that all the ajax requests in WP have to pass form the native WP AJAX handler ) but I sorted it out.
I think to start with something like:
var x = document.getElementById("my_file_upload");
if ('files' in x) {
if (x.files.length == 0) {
txt = "No file selected";
} else {
for (var i = 0; i < x.files.length; i++) {
// make an ajax request here for the first file,
// then when get the answer, take away the first file from the array and do
//the same to the second and so on...
As said I'm good in php but pretty new to code in WP, so I'm asking if somebody has made something similiar and have any suggestion that can be usefull to save time.