{"id":19036000,"date":"2026-10-04T05:00:30","date_gmt":"2026-10-04T05:00:30","guid":{"rendered":"https:\/\/wordpress.org\/support\/topic\/file-naming-bug-when-using-output-mapping\/"},"modified":"2026-10-04T05:00:30","modified_gmt":"2026-10-04T05:00:30","slug":"file-naming-bug-when-using-output-mapping","status":"publish","type":"topic","link":"https:\/\/wordpress.org\/support\/topic\/file-naming-bug-when-using-output-mapping\/","title":{"rendered":"File naming bug when using output mapping"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Context: we&#8217;re using a filter to set the image editor output format to webp. Originals are uploaded as jpeg or png and resized versions are all generated as webp.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">add_filter(&#8216;image_editor_output_format&#8217;, function (array $formats): array {<br \/>    $formats[&#8216;image\/jpeg&#8217;] = &#8216;image\/webp&#8217;;<br \/>    $formats[&#8216;image\/png&#8217;] = &#8216;image\/webp&#8217;;<br \/>     return $formats;<br \/>});<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Two bugs when an <code>image_editor_output_format<\/code> mapping is active (e.g. JPEG \u2192 WebP). Crop Thumbnails 1.9.7, WordPress 7.1.2<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>1. Crop filenames keep the source extension<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With JPEG \u2192 WebP mapped, cropping <code>photo.jpg<\/code> creates <code>photo.jpg-150x150.webp<\/code>. WordPress&#8217;s own sub-size would be <code>photo-150x150.webp<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Cause: in <code>CptSaveThumbnail::generateFilename()<\/code>, <code>$ext<\/code> is switched to the output extension (<code>webp<\/code>) before the base name is taken. <code>wp_basename($file, '.webp')<\/code> doesn&#8217;t match <code>photo.jpg<\/code>, so <code>.jpg<\/code> stays in the name. Taking the base name with the original extension before swapping <code>$ext<\/code> fixes it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>2. Re-cropping a size can delete a file another size still uses<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">WordPress gives sizes with identical dimensions one shared file. For example, <code>square<\/code> (1024\u00d71024 crop) and <code>large_square<\/code> (1024\u00d71024 crop) both point to <code>photo-1024x1024.jpg<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Steps:<\/p>\n\n\n\n<ol>\n<li>Crop&nbsp;<code>square<\/code>&nbsp;only.<\/li>\n\n\n\n<li>The crop gets a new name,&nbsp;<code>photo.jpg-1024x1024.webp<\/code>, so&nbsp;<code>filter_shouldDeleteOldFile()<\/code>&nbsp;returns true and&nbsp;<code>photo-1024x1024.jpg<\/code>&nbsp;is deleted.<\/li>\n\n\n\n<li><code>large_square<\/code>&nbsp;still points at&nbsp;<code>photo-1024x1024.jpg<\/code>, which no longer exists, so it renders as a broken image.<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">This happens whenever the crop&#8217;s filename differs from the old one, which is every crop on a site using an output-format mapping. Suggested fix: before deleting, check that no other size in the attachment metadata references the same file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Workarounds we&#8217;re using in the meantime: a <code>crop_thumbnails_filename<\/code> filter that strips the stray extension, and a <code>crop_thumbnails_should_delete_old_file<\/code> filter at priority 20 that keeps files still referenced by another size.<\/p>\n","protected":false},"template":"","class_list":["post-19036000","topic","type-topic","status-publish","hentry"],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19036000","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic"}],"about":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/types\/topic"}],"version-history":[{"count":0,"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/topic\/19036000\/revisions"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/support\/wp-json\/wp\/v2\/media?parent=19036000"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}