• Resolved m4rcu5

    (@m4rcu5)


    Hi,

    I noticed that when renaming/moving images, the various different sizes/thumbnails are physically moved, but the paths in the _wp_attachment_metadata filed are not updated.
    This breaks the srcset for responsive images.

    Taking a peek at the source, it seems like this should be handled by the move function in classes/core.php, but dumping the $meta at the end of the function still shows only the old paths. (eg, [file] => myimage.jpg, instead of [file] => mypath/myimage.jpg)

    This is using WordPress 5.8.2.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Jordy Meow

    (@tigroumeow)

    Hi @m4rcu5,

    This is a very basic feature of the plugin, and it has always worked; so I wonder why it’s not working in your case.

    The code that updates the metadata is located from the “// Image Sizes (Thumbnails)” comment in the core.php file. You can look at this, but of course, it does it ??

    Did you check your PHP Error Logs?

    Thread Starter m4rcu5

    (@m4rcu5)

    Hi @tigroumeow,

    After some more digging, it seems to affect only images that are moved from the root of the uploads folder. In this site, I do not have month-year based folders enabled, meaning everything is uploaded to the root.

    I think the following is going on (all in classes/core.php);
    [line 699] $old_directory = trim( str_replace( $upload_dir['basedir'], '', $path_parts['dirname'] ), '/' ); // '2011/01'
    It will try to get the old path (/my/path/to/uploads/ => / => “”)

    [line 727] $meta['file'] = $this->str_replace( $old_directory, $new_directory, $meta['file'] );
    Calling str_replace on line 610:

    
    function str_replace( $needle, $replace, $haystack ) {                                                                                                                                                      
    ?       ?       $pos = strpos( $haystack, $needle );
    ?       ?       if ( $pos !== false )
    ?       ?       ?       $haystack = substr_replace( $haystack, $replace, $pos, strlen( $needle ) );
    ?       ?       return $haystack;
    

    But as our needle is empty (“”), because there was no date based directory, there is no substitution, and therefore the original unaltered haystack (path) is returned, and no metadata is updated.

    I came up with the following patch that works for me:

    
    --- a/classes/core.php	2021-11-13 00:47:12.000000000 +0100
    +++ b/classes/core.php	2021-11-19 13:58:35.326454709 +0100
    @@ -723,11 +723,21 @@
     		$meta = wp_get_attachment_metadata( $id );
     
     		if ( $meta ) {
    -			if ( isset( $meta['file'] ) && !empty( $meta['file'] ) )
    +			if ( isset( $meta['file'] ) && !empty( $meta['file'] ) ) {
    +			    if ( $old_directory ) {
     				$meta['file'] = $this->str_replace( $old_directory, $new_directory, $meta['file'] );
    -			if ( isset( $meta['url'] ) && !empty( $meta['url'] ) && strlen( $meta['url'] ) > 4 )
    +			    } else {
    +				$meta['file'] = trailingslashit( $new_directory ) . $meta['file'];
    +			    }
    +			}
    +			if ( isset( $meta['url'] ) && !empty( $meta['url'] ) && strlen( $meta['url'] ) > 4 ) {
    +			    if ( $old_directory ) {
     				$meta['url'] = $this->str_replace( $old_directory, $new_directory, $meta['url'] );
    -			//wp_update_attachment_metadata( $id, $meta );
    +			    } else {
    +				$meta['url'] = trailingslashit( $new_directory ) . $meta['url'];
    +			    }
    +			}
    +                        //wp_update_attachment_metadata( $id, $meta );
     		}
     
     		// Better to check like this rather than with wp_attachment_is_image
    
    • This reply was modified 3 years, 3 months ago by m4rcu5. Reason: Fixing code block formatting
    Plugin Author Jordy Meow

    (@tigroumeow)

    Hi @m4rcu5,

    Thanks a lot for the debugging. I would like to dig into this a bit further, and make sure it works for you and everyone ??

    Just to make sure… did you get into issues because you basically unchecked the “Organize my uploads into month- and year-based folders” option? Or are you doing something else as well?

    Hi @tigroumeow,

    I seem to have the same issue. My files are sorted in the default uploads/YYYY/MM/ folders. But only the original is renamed, the thumbnail files are left untouched.

    Furthermore this is happening only sometimes and with some images. Others are resized / renamed fine.

    Do you have any idea where I might look further? Any setting I might have overlooked?

    Thank you very much for your help!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Thumbnail paths not updated’ is closed to new replies.