Title: wp_get_attachment_image_url
Published: December 9, 2015
Last modified: August 20, 2026

---

# wp_get_attachment_image_url( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): string|false

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#wp--skip-link--target)

Gets the URL of an image attachment.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#parameters)󠁿

 `$attachment_id`intrequired

Image attachment ID.

`$size`string|int[]optional

Image size. Accepts any registered image size name, or an array of width and height
values in pixels (in that order). Default `'thumbnail'`.

Default:`'thumbnail'`

`$icon`booloptional

Whether the image should be treated as an icon.

Default:`false`

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#return)󠁿

 string|false Attachment URL or false if no image is available. If `$size` does 
not match any registered image size, the original image URL will be returned.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#source)󠁿

    ```php
    function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) {
    	$image = wp_get_attachment_image_src( $attachment_id, $size, $icon );
    	return $image[0] ?? false;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/media.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/7.1/src/wp-includes/media.php#L1331)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.1/src/wp-includes/media.php#L1331-L1334)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#related)󠁿

| Uses | Description | 
| [wp_get_attachment_image_src()](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/)`wp-includes/media.php` |

Retrieves an image to represent an attachment.

  |

| Used by | Description | 
| [get_the_post_thumbnail_url()](https://developer.wordpress.org/reference/functions/get_the_post_thumbnail_url/)`wp-includes/post-thumbnail-template.php` |

Returns the post thumbnail URL.

  | 
| [get_site_icon_url()](https://developer.wordpress.org/reference/functions/get_site_icon_url/)`wp-includes/general-template.php` |

Returns the Site Icon URL.

  | 
| [wp_get_attachment_thumb_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_thumb_url/)`wp-includes/post.php` |

Retrieves URL for an attachment thumbnail.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#changelog)󠁿

| Version | Description | 
| [4.4.0](https://developer.wordpress.org/reference/since/4.4.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#comment-content-5662)
 2.    [MAIDOT](https://profiles.wordpress.org/maidot/)  [  5 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/#comment-5662)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-5662)
     Vote results for this note: 6[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-5662)
 4.  For whoever, like me, is looking for the standard sizes, here they are:
 5.  `thumbnail` = Thumbnail (default 150px x 150px max)
      `medium` = Medium resolution(
     default 300px x 300px max) `large` = Large resolution (default 1024px x 1024px
     max) `full` = Full resolution (original size uploaded)
 6.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%3Freplytocom%3D5662%23feedback-editor-5662)
 7.   [Skip to note 5 content](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#comment-content-1590)
 8.    [Rinku Y](https://profiles.wordpress.org/rinkuyadav999/)  [  10 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/#comment-1590)
 9.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-1590)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-1590)
 10.     ```php
         add_theme_support( 'post-thumbnails' );
         add_image_size( 'home-slide-img-mobile', 640, 1072, true ); //resize, crop in functions.php
         ```
     
 11. Now let display somewhere:
 12.     ```php
         $imgid = 6; //need to get it dynamically
         $imgurldesktop = wp_get_attachment_image_url( $imgid, '' ); //use default image size
         $imgurlmobile = wp_get_attachment_image_url( $imgid, 'home-slide-img-mobile' ); //use custom set size
         ```
     
 13.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%3Freplytocom%3D1590%23feedback-editor-1590)
 14.  [Skip to note 6 content](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/?output_format=md#comment-content-4160)
 15.   [David Elstob](https://profiles.wordpress.org/davwaiguy/)  [  6 years ago  ](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/#comment-4160)
 16. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-4160)
     Vote results for this note: 1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%23comment-4160)
 17.     ```php
         <?php $attachment_image = wp_get_attachment_url( get_post_thumbnail_id() ); ?>
         <link rel="preload" as="image" href="<?php echo esc_url( $attachment_image ); ?>">
         ```
     
 18. Add this snippet to the head section in header.php to preload the attachment image
     on every page/post.
 19.  * Recommend performing a check that the current page/post has a post thumbnail
        before adding the preload directive.
      * [crstauf](https://profiles.wordpress.org/crstauf/) [6 years ago](https://developer.wordpress.org/reference/functions/wp_get_attachment_image_url/#comment-4169)
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F%3Freplytocom%3D4160%23feedback-editor-4160)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_get_attachment_image_url%2F)
before being able to contribute a note or feedback.