I need to rename the raw file name from camera like DCM4819032.jpg
or 2015-12-11-204213.jpg
to more organized one like IMG_00015.jpg
.
I figured I will use the attachment ID as the number in that name.
So far I found a way to change the file name before uploading, but no access to the upcoming ID.
add_filter("wp_handle_upload_prefilter", function ($file) {
$file["name"] = "custom-name.jpg";
return $file;
}, 1, 1);
Is there a way to know what ID this attachment going to have?
[EDIT]
Found a way from this question, it works, but it disables image resizing, which is important in my case.
add_action("add_attachment", function($file_id) {
$file = get_attached_file($file_id);
$path = pathinfo($file);
$newfile_name = "IMG_" . $file_id . "." . $path["extension"];
$newfile = $path["dirname"] . "/" . $newfile_name;
rename($file, $newfile);
update_attached_file($file_id, $newfile);
});
From the comments, it says "WordPress won't understand that a rename has happened", which might be the reason why the resizing isn't working.
DCM859132.jpg
, it would be better if the file name is00015.jpg
orIMG_00015.jpg
00015.jpg
is not better thanDCM859132.jpg
for any reason I can think of.