WordPress.org

Make WordPress Core

Ticket #18855: get_attached_file-returns-false.diff

File get_attached_file-returns-false.diff, 1.1 KB (added by mikeschinkel, 7 years ago)

Fixes get_attached_file() so that it returns false when the attachment_id is not valid.

  • post.php

     
    168168 *
    169169 * @param int $attachment_id Attachment ID.
    170170 * @param bool $unfiltered Whether to apply filters.
    171  * @return string The file path to the attached file.
     171 * @return string|bool The file path to the attached file, or false if the attachment does not exist.
    172172 */
    173173function get_attached_file( $attachment_id, $unfiltered = false ) {
    174174        $file = get_post_meta( $attachment_id, '_wp_attached_file', true );
    175         // If the file is relative, prepend upload dir
    176         if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
    177                 $file = $uploads['basedir'] . "/$file";
     175        if ( ! empty ( $file ) ) {
     176                // If the file is relative, prepend upload dir
     177                if ( 0 !== strpos($file, '/') && !preg_match('|^.:\\\|', $file) && ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) )
     178                        $file = $uploads['basedir'] . "/$file";
     179        }
    178180        if ( $unfiltered )
    179181                return $file;
    180182        return apply_filters( 'get_attached_file', $file, $attachment_id );