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

Get the URL of an image attachment.

Parameters Parameters


(int) (Required) Image attachment ID.


(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 value: 'thumbnail'


(bool) (Optional) Whether the image should be treated as an icon.

Default value: false

Top ↑

Return 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.

Top ↑

Source Source

File: wp-includes/media.php

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

Top ↑

Changelog Changelog

Version Description
4.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Rinku Y
    add_theme_support( 'post-thumbnails' );
    add_image_size( 'home-slide-img-mobile', 640, 1072, true ); //resize, crop in functions.php

    Now let display somewhere:

    $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
  2. Skip to note 2 content
    Contributed by David Elstob
    <?php $attachment_image = wp_get_attachment_url( get_post_thumbnail_id() ); ?>
    <link rel="preload" as="image" href="<?php echo esc_url( $attachment_image ); ?>">

    Add this snippet to the head section in header.php to preload the attachment image on every page/post.

You must log in before being able to contribute a note or feedback.