Hey, I don’t plan adding a feature like this to the plug-in, but I can provide some lines of custom code that allow you to accomplish this task.
Would it be enough to provide a list of original images and provide a link to each attachment so you can delete it? Or would there be a need for some kind of time-based condition? Eg. only show images that are older than 30 days?
Thanks!
Actually, disregard my previous message, I didn’t realize you would also need to backup the images. I will see if I can figure out a way to attach a filter to the file upload process so the upload folder can be modified.
Hey, here’s the code I managed to put together.
I worked in my testing but I would recommend you to test the code in a local or a staging environment before deploying it to production.
Please note that the code will only work for images uploaded through the front-end forms. Images uploaded in the WordPress admin use a different way of uploading.
add_filter('upload_dir', function ($uploads){
global $wp;
if (defined('REST_REQUEST') && REST_REQUEST && !empty($wp->request) && stripos($wp->request, 'wp-json/aiarc/v1/upload') !== false) {
$custom_dir_name = 'my-custom-dir-here';
if (!empty($uploads['subdir'])) {
$new_subdir = '/' . $custom_dir_name . $uploads['subdir'];
$uploads['subdir'] = $new_subdir;
$uploads['path'] = $uploads['basedir'] . $new_subdir;
$uploads['url'] = $uploads['baseurl'] . $new_subdir;
} else {
$uploads['subdir'] = '/' . $custom_dir_name;
$uploads['path'] = $uploads['path'] . $uploads['subdir'];
$uploads['url'] = $uploads['url'] . $uploads['subdir'];
}
}
return $uploads;
}, 10, 1);
This code should go in your theme or a custom plugin.
-
This reply was modified 4 years ago by
joppuyo.
Thread Starter
kkriss
(@kkriss)
Hi joppuyo.
Very happy you did what I need! I’m not so advanced, so I’m asking you:
1) Should I add the code to my functions.php?
2) Have to substitute only once my folder name (see original-images-submitted)?
add_filter('upload_dir', function ($uploads){
global $wp;
if (defined('REST_REQUEST') && REST_REQUEST && !empty($wp->request) && stripos($wp->request, 'wp-json/aiarc/v1/upload') !== false) {
$custom_dir_name = 'original-images-submitted';
if (!empty($uploads['subdir'])) {
$new_subdir = '/' . $custom_dir_name . $uploads['subdir'];
$uploads['subdir'] = $new_subdir;
$uploads['path'] = $uploads['basedir'] . $new_subdir;
$uploads['url'] = $uploads['baseurl'] . $new_subdir;
} else {
$uploads['subdir'] = '/' . $custom_dir_name;
$uploads['path'] = $uploads['path'] . $uploads['subdir'];
$uploads['url'] = $uploads['url'] . $uploads['subdir'];
}
}
return $uploads;
}, 10, 1);
/
3) if something will go wrong is it sufficient I delete this code? I’m working on a subdomain, I’m near to the end before publishing. I have some backups but hope not to have to restore them.
Thank you so much
-
This reply was modified 4 years ago by
kkriss.
-
This reply was modified 4 years ago by
kkriss.
Yes, you can put the code in the theme functions.php file.¨
Your code looks good to me.
If there are any issues with the code, it is enough to delete the code in this case.
Thread Starter
kkriss
(@kkriss)
Hi joppuyo.
I added the code to my functions and uploaded an image through a front-end form. The original image and the cropped one are still uploaded in the WP media library (see here).
Should I create the “original-images-submitted” folder before naming it in the code you sent me or it should be automatically created if not present? Please guide me, I’m not so advanced.
Thank you so much
Can you check where the file is stored on the server? Regardless of the folder where the image files are stored, the image will show up in the media library.
Thread Starter
kkriss
(@kkriss)
Here it is
In the media library will I see both the original and the cropped image? Could I manage those images or only see?
You said that you want to upload the original image to a folder named “original-images-submitted” ? Could you check that the images are stored in that folder? You can use something like a FTP client to do this.
Thread Starter
kkriss
(@kkriss)
The folder should be in wp-content? Could I send you my content in private?
Thread Starter
kkriss
(@kkriss)
I mean “wp-content screenshot