wp_get_attachment_image_src

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

WordPress Version: 6.5

/**
 * Retrieves an image to represent an attachment.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
 *                                    width and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
 * @return array|false {
 *     Array of image data, or boolean false if no image is available.
 *
 *     @type string $0 Image source URL.
 *     @type int    $1 Image width in pixels.
 *     @type int    $2 Image height in pixels.
 *     @type bool   $3 Whether the image is a resized image.
 * }
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // Get a thumbnail or intermediate image if there is one.
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id, '.svg');
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = wp_getimagesize($src_file);
                $ext = strtolower(substr($src_file, -4));
                if ('.svg' === $ext) {
                    // SVG does not have true dimensions, so this assigns width and height directly.
                    $width = 48;
                    $height = 64;
                } else {
                    list($width, $height) = wp_getimagesize($src_file);
                }
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height, false);
        }
    }
    /**
     * Filters the attachment image source result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         {
     *     Array of image data, or boolean false if no image is available.
     *
     *     @type string $0 Image source URL.
     *     @type int    $1 Image width in pixels.
     *     @type int    $2 Image height in pixels.
     *     @type bool   $3 Whether the image is a resized image.
     * }
     * @param int          $attachment_id Image attachment ID.
     * @param string|int[] $size          Requested image size. Can be any registered image size name, or
     *                                    an array of width and height values in pixels (in that order).
     * @param bool         $icon          Whether the image should be treated as an icon.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 5.7

/**
 * Retrieves an image to represent an attachment.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
 *                                    width and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
 * @return array|false {
 *     Array of image data, or boolean false if no image is available.
 *
 *     @type string $0 Image source URL.
 *     @type int    $1 Image width in pixels.
 *     @type int    $2 Image height in pixels.
 *     @type bool   $3 Whether the image is a resized image.
 * }
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // Get a thumbnail or intermediate image if there is one.
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id);
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = wp_getimagesize($src_file);
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height, false);
        }
    }
    /**
     * Filters the attachment image source result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         {
     *     Array of image data, or boolean false if no image is available.
     *
     *     @type string $0 Image source URL.
     *     @type int    $1 Image width in pixels.
     *     @type int    $2 Image height in pixels.
     *     @type bool   $3 Whether the image is a resized image.
     * }
     * @param int          $attachment_id Image attachment ID.
     * @param string|int[] $size          Requested image size. Can be any registered image size name, or
     *                                    an array of width and height values in pixels (in that order).
     * @param bool         $icon          Whether the image should be treated as an icon.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 5.6

/**
 * Retrieves an image to represent an attachment.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|int[] $size          Optional. Image size. Accepts any registered image size name, or an array of
 *                                    width and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
 * @return array|false {
 *     Array of image data, or boolean false if no image is available.
 *
 *     @type string $0 Image source URL.
 *     @type int    $1 Image width in pixels.
 *     @type int    $2 Image height in pixels.
 *     @type bool   $3 Whether the image is a resized image.
 * }
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // Get a thumbnail or intermediate image if there is one.
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id);
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = @getimagesize($src_file);
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height, false);
        }
    }
    /**
     * Filters the attachment image source result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         {
     *     Array of image data, or boolean false if no image is available.
     *
     *     @type string $0 Image source URL.
     *     @type int    $1 Image width in pixels.
     *     @type int    $2 Image height in pixels.
     *     @type bool   $3 Whether the image is a resized image.
     * }
     * @param int          $attachment_id Image attachment ID.
     * @param string|int[] $size          Requested image size. Can be any registered image size name, or
     *                                    an array of width and height values in pixels (in that order).
     * @param bool         $icon          Whether the image should be treated as an icon.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 5.5

/**
 * Retrieves an image to represent an attachment.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|int[] $size          Optional. Image size. Accepts any valid image size name, or an array of width
 *                                    and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
 * @return array|false {
 *     Array of image data, or boolean false if no image is available.
 *
 *     @type string $0 Image source URL.
 *     @type int    $1 Image width in pixels.
 *     @type int    $2 Image height in pixels.
 *     @type bool   $3 Whether the image is a resized image.
 * }
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // Get a thumbnail or intermediate image if there is one.
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id);
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = @getimagesize($src_file);
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height, false);
        }
    }
    /**
     * Filters the attachment image source result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         {
     *     Array of image data, or boolean false if no image is available.
     *
     *     @type string $0 Image source URL.
     *     @type int    $1 Image width in pixels.
     *     @type int    $2 Image height in pixels.
     *     @type bool   $3 Whether the image is a resized image.
     * }
     * @param int          $attachment_id Image attachment ID.
     * @param string|int[] $size          Requested size of image. Image size name, or array of width
     *                                    and height values (in that order).
     * @param bool         $icon          Whether the image should be treated as an icon.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 5.4

/**
 * Retrieves an image to represent an attachment.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|int[] $size          Optional. Image size. Accepts any valid image size name, or an array of width
 *                                    and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should fall back to a mime type icon. Default false.
 * @return array|false {
 *     Array of image data, or boolean false if no image is available.
 *
 *     @type string $0 Image source URL.
 *     @type int    $1 Image width in pixels.
 *     @type int    $2 Image height in pixels.
 * }
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // Get a thumbnail or intermediate image if there is one.
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id);
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = @getimagesize($src_file);
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height);
        }
    }
    /**
     * Filters the attachment image source result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         {
     *     Array of image data, or boolean false if no image is available.
     *
     *     @type string $0 Image source URL.
     *     @type int    $1 Image width in pixels.
     *     @type int    $2 Image height in pixels.
     * }
     * @param int          $attachment_id Image attachment ID.
     * @param string|int[] $size          Requested size of image. Image size name, or array of width
     *                                    and height values (in that order).
     * @param bool         $icon          Whether the image should be treated as an icon.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 5.3

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * The returned array contains four values: the URL of the attachment image src,
 * the width of the image file, the height of the image file, and a boolean
 * representing whether the returned array describes an intermediate (generated)
 * image size or the original, full-sized upload.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
 *                                    and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @return false|array Returns an array (url, width, height, is_intermediate), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon) {
            $src = wp_mime_type_icon($attachment_id);
            if ($src) {
                /** This filter is documented in wp-includes/post.php */
                $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
                $src_file = $icon_dir . '/' . wp_basename($src);
                list($width, $height) = @getimagesize($src_file);
            }
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height);
        }
    }
    /**
     * Filters the image src result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         Either array with src, width & height, icon src, or false.
     * @param int          $attachment_id Image attachment ID.
     * @param string|array $size          Size of image. Image size or array of width and height values
     *                                    (in that order). Default 'thumbnail'.
     * @param bool         $icon          Whether the image should be treated as an icon. Default false.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 4.6

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * The returned array contains four values: the URL of the attachment image src,
 * the width of the image file, the height of the image file, and a boolean
 * representing whether the returned array describes an intermediate (generated)
 * image size or the original, full-sized upload.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
 *                                    and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @return false|array Returns an array (url, width, height, is_intermediate), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon && $src = wp_mime_type_icon($attachment_id)) {
            /** This filter is documented in wp-includes/post.php */
            $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
            $src_file = $icon_dir . '/' . wp_basename($src);
            @list($width, $height) = getimagesize($src_file);
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height);
        }
    }
    /**
     * Filters the image src result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         Either array with src, width & height, icon src, or false.
     * @param int          $attachment_id Image attachment ID.
     * @param string|array $size          Size of image. Image size or array of width and height values
     *                                    (in that order). Default 'thumbnail'.
     * @param bool         $icon          Whether the image should be treated as an icon. Default false.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 4.4

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * The returned array contains four values: the URL of the attachment image src,
 * the width of the image file, the height of the image file, and a boolean
 * representing whether the returned array describes an intermediate (generated)
 * image size or the original, full-sized upload.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Image size. Accepts any valid image size, or an array of width
 *                                    and height values in pixels (in that order). Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @return false|array Returns an array (url, width, height, is_intermediate), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon && $src = wp_mime_type_icon($attachment_id)) {
            /** This filter is documented in wp-includes/post.php */
            $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
            $src_file = $icon_dir . '/' . wp_basename($src);
            @list($width, $height) = getimagesize($src_file);
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height);
        }
    }
    /**
     * Filter the image src result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         Either array with src, width & height, icon src, or false.
     * @param int          $attachment_id Image attachment ID.
     * @param string|array $size          Size of image. Image size or array of width and height values
     *                                    (in that order). Default 'thumbnail'.
     * @param bool         $icon          Whether the image should be treated as an icon. Default false.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 4.3

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Registered image size to retrieve the source for or a flat
 *                                    array of height and width dimensions. Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @return false|array Returns an array (url, width, height), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    $image = image_downsize($attachment_id, $size);
    if (!$image) {
        $src = false;
        if ($icon && $src = wp_mime_type_icon($attachment_id)) {
            /** This filter is documented in wp-includes/post.php */
            $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
            $src_file = $icon_dir . '/' . wp_basename($src);
            @list($width, $height) = getimagesize($src_file);
        }
        if ($src && $width && $height) {
            $image = array($src, $width, $height);
        }
    }
    /**
     * Filter the image src result.
     *
     * @since 4.3.0
     *
     * @param array|false  $image         Either array with src, width & height, icon src, or false.
     * @param int          $attachment_id Image attachment ID.
     * @param string|array $size          Registered image size to retrieve the source for or a flat
     *                                    array of height and width dimensions. Default 'thumbnail'.
     * @param bool         $icon          Whether the image should be treated as an icon. Default false.
     */
    return apply_filters('wp_get_attachment_image_src', $image, $attachment_id, $size, $icon);
}

WordPress Version: 4.2

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * @since 2.5.0
 *
 * @param int          $attachment_id Image attachment ID.
 * @param string|array $size          Optional. Registered image size to retrieve the source for or a flat
 *                                    array of height and width dimensions. Default 'thumbnail'.
 * @param bool         $icon          Optional. Whether the image should be treated as an icon. Default false.
 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    if ($image = image_downsize($attachment_id, $size)) {
        return $image;
    }
    $src = false;
    if ($icon && $src = wp_mime_type_icon($attachment_id)) {
        /** This filter is documented in wp-includes/post.php */
        $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
        $src_file = $icon_dir . '/' . wp_basename($src);
        @list($width, $height) = getimagesize($src_file);
    }
    if ($src && $width && $height) {
        return array($src, $width, $height);
    }
    return false;
}

WordPress Version: 3.9

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * @since 2.5.0
 *
 * @param int $attachment_id Image attachment ID.
 * @param string $size Optional, default is 'thumbnail'.
 * @param bool $icon Optional, default is false. Whether it is an icon.
 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    if ($image = image_downsize($attachment_id, $size)) {
        return $image;
    }
    $src = false;
    if ($icon && $src = wp_mime_type_icon($attachment_id)) {
        /** This filter is documented in wp-includes/post.php */
        $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/media');
        $src_file = $icon_dir . '/' . wp_basename($src);
        @list($width, $height) = getimagesize($src_file);
    }
    if ($src && $width && $height) {
        return array($src, $width, $height);
    }
    return false;
}

WordPress Version: 3.7

/**
 * Retrieve an image to represent an attachment.
 *
 * A mime icon for files, thumbnail or intermediate size for images.
 *
 * @since 2.5.0
 *
 * @param int $attachment_id Image attachment ID.
 * @param string $size Optional, default is 'thumbnail'.
 * @param bool $icon Optional, default is false. Whether it is an icon.
 * @return bool|array Returns an array (url, width, height), or false, if no image is available.
 */
function wp_get_attachment_image_src($attachment_id, $size = 'thumbnail', $icon = false)
{
    // get a thumbnail or intermediate image if there is one
    if ($image = image_downsize($attachment_id, $size)) {
        return $image;
    }
    $src = false;
    if ($icon && $src = wp_mime_type_icon($attachment_id)) {
        $icon_dir = apply_filters('icon_dir', ABSPATH . WPINC . '/images/crystal');
        $src_file = $icon_dir . '/' . wp_basename($src);
        @list($width, $height) = getimagesize($src_file);
    }
    if ($src && $width && $height) {
        return array($src, $width, $height);
    }
    return false;
}