wp-auto-upload
wp-auto-upload copied to clipboard
Image gets saved to file system, but incorrect image path is saved in media attachment
I have a strange behavior when using this plugin, I have imported posts from a Squarespace blog. I import the posts, and the image paths before the plugin is activated are like this:
https://images.squarespace-cdn.com/content/v1/55b3d4afe4b0edbb36a4b6ea/1552236764572-KT58HNL34WDKJYMWFJFX/ke17ZwdGBToddI8pDm48kLzw7P_nKYlGGC0MzqILKf17gQa3H78H3Y0txjaiv_0fDoOvxcdMmMKkDsyUqMSsMWxHk725yiiHCCLfrh8O1z4YTzHvnKhyp6Da-NYroOW3rv7sVmavAApj1cuSnuUyXzqqXjS3CfNDSuuf31e0tVEhpqFMoPGd0_ypppePcJDz-vqyAZGPnUfr_Ql19q21eghW3lgGaAnbOfVbqEfQJYA/Accounts+Payable300dpi--3.jpg?format=original
When I activate the wp-auto-upload plugin, the path is like this:
.../uploads/2019/10AccountsPayable300dpi-3.jpg
The image is saved to disk under this path:
.../uploads/2019/10/AccountsPayable300dpi-3.jpg
It should be uploads/year/month/image. Instead the path is saved as uploads/year/monthimage (no slash between month and image name)
What is strange is, the image path does get overwritten properly in the edited post, where the original image url got replaced with the imported image url. But since the image url doesn't get saved to the media attachment, you are unable to see the image in the media gallery (a blank thumbnail) and you can not use the image in a different post / page.
I'm using the latest version of the plugin from wordpress.org and the latest stable wordpress release ( 5.2.4 ) I have the classic editor plugin installed if that's relevant. OS is windows, server is Apache.
I was able to get around this issue by temporarily disabling the "Organize my uploads into month- and year-based folders" setting.
Still keeping this issue open as the current plugin breaks with the default setting. (I'd wager this is some kind of regex weirdness going on)
Just to help out people in the future who have the same issue: the "Organize my uploads into month-..." option is located under general WP Media settings (/wp-admin/options-media.php), not in this plugin-specific settings.
I found a solution, replace the following code https://github.com/airani/wp-auto-upload/blob/1305af69b07e828c5407e24e2d4c5485bf95899e/src/ImageUploader.php#L256-L260
by
$attach_id = wp_insert_attachment($attachment, $image['base_path'] . '/' . $image['filename'], $this->post['ID']);
if (!function_exists('wp_generate_attachment_metadata')) {
include_once( ABSPATH . 'wp-admin/includes/image.php' );
}
$attach_data = wp_generate_attachment_metadata($attach_id, $image['base_path'] . '/' . $image['filename']);
Because $image['path'] = $image['base_path'] . DIRECTORY_SEPARATOR . $image['filename'], it make wrong path when inserting the attachments.
I couldn't test and debug this problem and I think probably it happens in Windows OS also it's not clear enough for me
I have some questions about this problem:
- It's problem only for attachment path?
- Every image urls have this problem or just for specific url?
This is the same problem, the proposed solution on WP.org is working for me. It's not Windows dependent.
https://wordpress.org/support/topic/uploaded-images-not-showing-in-the-media-library/
If you need to sanitize your database, I ran this query, then the images start showing up correctly under WP -> Admin -> Media and in gallery posts.
This will only work on MariaDB. Make a database backup before you run it!
UPDATE wp_postmeta a
SET meta_value = REGEXP_REPLACE(a.meta_value, '20([0-9]{2})/([0-9]{2})', '20\\1/\\2/')
where exists (select *
from wp_posts b where a.post_id=b.ID
and b.post_type='attachment')
and a.meta_key='_wp_attached_file'
and a.meta_value REPLACE(REGEXP_REPLACE(a.meta_value, '20([0-9]{2})/([0-9]{2})', '20\\1/\\2/'), '//','/');
Updating the _wp_attachment_metadata records will break things, do not update it. Only update the _wp_attached_file as done above.