I managed to solve this after being pointed in the right direction. In my case, I filtered wp_get_attachment_image_attributes
to change all the paths in the admin for attachments at once.
In my case, I am using descriptive suffixes on all my image files (i.e. _featured
etc) so modifying them was easy using strtr
which I also used to change the extension from jpg to webp.
Now all attachment image previews load in the files I want in the correct format at the desired size, and it works for Media Library, featured images, and custom fields.
add_filter( 'wp_get_attachment_image_attributes', 'my_attachment_filter', 10, 3 );
function my_attachment_filter($attr, $attachment, $size){
if (is_admin()){
if (array_key_exists( 'src' , $attr)){
$old_src = $attr['src'];
$new_src = strtr($old_src, array('_featured' => '_featured_eighth', '_portrait' => '_portrait_204', '_og.' => '_og_320.', '.jpg' => '.webp'));
$attr['src'] = $new_src;
}
}
return $attr;
}