Skip to content

Commit 010a481

Browse files
committed
Media: Add argument to get_attached_file() for subsizes.
Add a `$size` argument to `get_attached_file()` to simplify getting the path to an intermediate image size. Props paulschreiber, audrasjb, Mista-Flo. Fixes #51780. Built from https://develop.svn.wordpress.org/trunk@55199 git-svn-id: https://core.svn.wordpress.org/trunk@54732 1a063a9b-81f0-0310-95a4-ce76da25c4cd
1 parent 1552c50 commit 010a481

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

wp-includes/post.php

+20-4
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ function create_initial_post_types() {
705705
/**
706706
* Retrieves attached file path based on attachment ID.
707707
*
708+
* Will return intermediate size path if $size param is provided.
709+
*
708710
* By default the path will go through the 'get_attached_file' filter, but
709711
* passing a true to the $unfiltered argument of get_attached_file() will
710712
* return the file path unfiltered.
@@ -715,13 +717,27 @@ function create_initial_post_types() {
715717
* attached filename through a filter.
716718
*
717719
* @since 2.0.0
720+
* @since 6.2 The `$size` parameter was added
721+
*
722+
* @param int $attachment_id Attachment ID.
723+
* @param bool $unfiltered Optional. Whether to apply filters. Default false.
724+
* @param string|int[] $size Optional. Image size. Accepts any registered image size name, or an array
725+
* of width and height values in pixels (in that order). Default ''.
718726
*
719-
* @param int $attachment_id Attachment ID.
720-
* @param bool $unfiltered Optional. Whether to apply filters. Default false.
721727
* @return string|false The file path to where the attached file should be, false otherwise.
722728
*/
723-
function get_attached_file( $attachment_id, $unfiltered = false ) {
724-
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
729+
function get_attached_file( $attachment_id, $unfiltered = false, $size = '' ) {
730+
731+
// Check for intermediate sizes first, otherwise fallback to original attachment size
732+
if ( ! empty( $size ) ) {
733+
$intermediate_image = image_get_intermediate_size( $attachment_id, $size );
734+
if ( ! $intermediate_image || ! isset( $intermediate_image['path'] ) ) {
735+
return false;
736+
}
737+
$file = $intermediate_image['path'];
738+
} else {
739+
$file = get_post_meta( $attachment_id, '_wp_attached_file', true );
740+
}
725741

726742
// If the file is relative, prepend upload dir.
727743
if ( $file && 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ) {

wp-includes/version.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @global string $wp_version
1818
*/
19-
$wp_version = '6.2-alpha-55198';
19+
$wp_version = '6.2-alpha-55199';
2020

2121
/**
2222
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.

0 commit comments

Comments
 (0)