Opened 2 months ago
#51780 new enhancement
Add ability to easily get path to sized images (such as parameter to get_attached_file() or wp_get_attachment_thumb_file())
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Awaiting Review | Priority: | normal |
Severity: | normal | Version: | 5.5.3 |
Component: | Media | Keywords: | |
Focuses: | Cc: |
Description
Currently, you can call get_attached_file()
and get the path to the full-sized version of the image.
However, if you want the path to a smaller variant of the image, you end up with something like this:
<?php $imagedata = wp_get_attachment_metadata( $image_id ); $file_path = get_attached_file( $image_id ); $thumbnail_file = str_replace( wp_basename( $file_path ), $imagedata['sizes'][ $size ]['file'], $file_path );
Or like this:
<?php $imagedata = image_get_intermediate_size( $image_id, $size ); $upload_directory = wp_get_upload_dir(); $thumbnail_file = $upload_directory['basedir'] . '/' . $imagedata['path'];
For brevity, these examples omit the null dereference checks, which triple the size of the code needed.
This is similar to the behaviour of wp_get_attachment_thumb_file()
, but that function hard-codes thumb
as the only size.
It would be very helpful if this existed:
<?php get_attached_file( int $attachment_id, bool $unfiltered = false, string $size = false )
Or this existed:
<?php wp_get_attachment_thumb_file( int $post_id, string $size = false )
See related tickets #33959 (from 2015) and #17262 (from 2011).
I'm happy to make a patch for either/both of these approaches, but don't know which one you prefer, and don't want this to languish for 11 years.