get_attached_file

The timeline below displays how wordpress function get_attached_file has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * Retrieves attached file path based on attachment ID.
 *
 * By default the path will go through the {@see 'get_attached_file'} filter, but
 * passing `true` to the `$unfiltered` argument will return the file path unfiltered.
 *
 * The function works by retrieving the `_wp_attached_file` post meta value.
 * This is a convenience function to prevent looking up the meta name and provide
 * a mechanism for sending the attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to skip the {@see 'get_attached_file'} filter.
 *                            Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && !str_starts_with($file, '/') && !preg_match('|^.:\\\\|', $file)) {
        $uploads = wp_get_upload_dir();
        if (false === $uploads['error']) {
            $file = $uploads['basedir'] . "/{$file}";
        }
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string|false $file          The file path to where the attached file should be, false otherwise.
     * @param int          $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 6.2

/**
 * Retrieves attached file path based on attachment ID.
 *
 * By default the path will go through the {@see 'get_attached_file'} filter, but
 * passing `true` to the `$unfiltered` argument will return the file path unfiltered.
 *
 * The function works by retrieving the `_wp_attached_file` post meta value.
 * This is a convenience function to prevent looking up the meta name and provide
 * a mechanism for sending the attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to skip the {@see 'get_attached_file'} filter.
 *                            Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file)) {
        $uploads = wp_get_upload_dir();
        if (false === $uploads['error']) {
            $file = $uploads['basedir'] . "/{$file}";
        }
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string|false $file          The file path to where the attached file should be, false otherwise.
     * @param int          $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 6.1

/**
 * Retrieves attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file)) {
        $uploads = wp_get_upload_dir();
        if (false === $uploads['error']) {
            $file = $uploads['basedir'] . "/{$file}";
        }
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string|false $file          The file path to where the attached file should be, false otherwise.
     * @param int          $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 5.5

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file)) {
        $uploads = wp_get_upload_dir();
        if (false === $uploads['error']) {
            $file = $uploads['basedir'] . "/{$file}";
        }
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string|false $file          The file path to where the attached file should be, false otherwise.
     * @param int          $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 5.3

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file)) {
        $uploads = wp_get_upload_dir();
        if (false === $uploads['error']) {
            $file = $uploads['basedir'] . "/{$file}";
        }
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 4.6

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_get_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filters the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 4.5

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_get_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filter the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 4.3

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|false The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filter the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 4.2

/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|bool The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filter the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 4.0

// highest priority
/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int  $attachment_id Attachment ID.
 * @param bool $unfiltered    Optional. Whether to apply filters. Default false.
 * @return string|bool The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir.
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filter the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 3.9

// highest priority
/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int $attachment_id Attachment ID.
 * @param bool $unfiltered Whether to apply filters.
 * @return string|bool The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    /**
     * Filter the attached file based on the given ID.
     *
     * @since 2.1.0
     *
     * @param string $file          Path to attached file.
     * @param int    $attachment_id Attachment ID.
     */
    return apply_filters('get_attached_file', $file, $attachment_id);
}

WordPress Version: 3.7

// highest priority
/**
 * Retrieve attached file path based on attachment ID.
 *
 * By default the path will go through the 'get_attached_file' filter, but
 * passing a true to the $unfiltered argument of get_attached_file() will
 * return the file path unfiltered.
 *
 * The function works by getting the single post meta name, named
 * '_wp_attached_file' and returning it. This is a convenience function to
 * prevent looking up the meta name and provide a mechanism for sending the
 * attached filename through a filter.
 *
 * @since 2.0.0
 *
 * @param int $attachment_id Attachment ID.
 * @param bool $unfiltered Whether to apply filters.
 * @return string|bool The file path to where the attached file should be, false otherwise.
 */
function get_attached_file($attachment_id, $unfiltered = false)
{
    $file = get_post_meta($attachment_id, '_wp_attached_file', true);
    // If the file is relative, prepend upload dir
    if ($file && 0 !== strpos($file, '/') && !preg_match('|^.:\\\\|', $file) && (($uploads = wp_upload_dir()) && false === $uploads['error'])) {
        $file = $uploads['basedir'] . "/{$file}";
    }
    if ($unfiltered) {
        return $file;
    }
    return apply_filters('get_attached_file', $file, $attachment_id);
}