You can try to replace
$arr['name'] = $post_slug . '-' . $random_number . '.jpg';
with
$arr['name'] = $post_slug . '-' . $arr['name'];
to get the file format [post_slug]-[original_filename].ext
.
Update:
Here is an example of the $arr
structure for an image with the filename car.png
:
Array
(
[name] => car.png
[type] => image/png
[tmp_name] => /tmp/phpJKhCwI
[error] => 0
[size] => 5868
)
To get the [post_slug].ext
format, one could use this:
$arr['name'] = $post_slug . '.' . pathinfo( $arr['name'], PATHINFO_EXTENSION );
When the post title is My favorite car
it becomes:
Array
(
[name] => my-favorite-car.png
[type] => image/png
[tmp_name] => /tmp/phpJKhCwI
[error] => 0
[size] => 5868
)
When more than one image is uploaded with the same filename, the uploaded image filename will become:
my-favorite-car.png (1. upload)
my-favorite-car1.png (2. upload)
my-favorite-car2.png (3. upload)
...