Hi there, sorry I was busy.
I have probably found the issue. If you take a look closely at what your plugin does, it simply rename the file on upload without changing its metadata. And because of that, the post created for the attachment (every single image has an internal wordpress post) has an url which references the name BEFORE the rename.
In short, you probably have to use wp_update_attachment_metadata() just after having renamed the image in order to update the image’s metadata (here, the post’s name).
Take a look at this example :
[215] => WP_Post Object
(
[ID] => 215
[post_author] => 1
[post_date] => 2016-12-10 20:46:16
[post_date_gmt] => 2016-12-10 19:46:16
[post_content] =>
[post_title] => Alternative - League of Legends - 03
[post_excerpt] =>
[post_status] => inherit
[comment_status] => open
[ping_status] => closed
[post_password] =>
[post_name] => league-of-legends-faire-un-penta-avec-le-mur-d-anivia-c-est-possible_20890_wide1
[to_ping] =>
[pinged] =>
[post_modified] => 2016-12-10 20:48:51
[post_modified_gmt] => 2016-12-10 19:48:51
[post_content_filtered] =>
[post_parent] => 126
[guid] => https://voluntary_hidden_path.jpg
[menu_order] => 0
[post_type] => attachment
[post_mime_type] => image/jpeg
[comment_count] => 0
[filter] => raw
)
Here is a part of the array extracted with get_attached_media( ‘image’ ). For this image, you can see all its related metadata. You can clearly see that [post_name] hasn’t been modified during upload and corresponds to the original filename BEFORE upload.
And because the standard permalink structure for images posts take this [post_name] for the url, this leads to a 404 error if you click on the link related to the post image.
The only thing you have to do is to update this [post_name] just AFTER the rename process in order to give it the same value as your sanitize_file_name (without omitting to add a $count=++ in order to avoid duplicates and potential error).
Do you get it?
-
This reply was modified 7 years, 11 months ago by moxymore.