WordPress wp_get_attachment_image_src() Function

Definition
In WordPress, wp_get_attachment_image_src() returns an array with the URL, width, and height of an image in the Media Library.
Use it when you need the numbers or the URL, for example for a CSS background or a meta tag.
Syntax
A few ways to call wp_get_attachment_image_src().
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
echo esc_url( $image[0] );
}
| Call | What it does |
|---|
wp_get_attachment_image_src( 15 ) | Use the thumbnail size. |
wp_get_attachment_image_src( 15, 'large' ) | Use the large size. |
wp_get_attachment_image_src( 15, 'full' ) | Use the original image. |
wp_get_attachment_image( 15 ) | Return a ready-made img tag instead. |
It returns an array: [0] is the URL, [1] is the width, [2] is the height, and [3] is true if the size is a resized copy.
Full declaration
wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): array|false
Parameters
| Parameter | Description |
|---|
$attachment_id | int. The ID of the image in the Media Library. |
$size | string|int[]. A size name or an array of width and height. Default is thumbnail. |
$icon | bool. true uses a media icon when the file is not an image. Default is false. |
Return Value
| Return Value | Description |
|---|
| array | An array: [0] URL, [1] width, [2] height, [3] is_intermediate. |
| false | If the image is not found. |
WordPress Examples
Three short examples. Each one shows the code and what it prints.
Example 1 — Get the image URL
Print only the URL.
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
echo esc_url( $image[0] );
}
https://example.com/wp-content/uploads/2026/11/sunset-300x200.jpg
How It Works
- The function returns an array.
$image[0] is the URL.esc_url() makes it safe to print.
Example 2 — Get the width and height
Use the other values of the array.
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
list( $url, $width, $height ) = $image;
echo $width . ' x ' . $height;
}
How It Works
list() splits the array into three variables.- The numbers are the size of the chosen image.
- We print them as
300 x 200.
Example 3 — Use it as a CSS background
Put the URL in an inline style.
<?php
$image = wp_get_attachment_image_src( 15, 'large' );
if ( $image ) :
?>
<div style="background-image: url('<?php echo esc_url( $image[0] ); ?>');">
Hero area
</div>
<?php endif; ?>
<div style="background-image: url('https://example.com/wp-content/uploads/2026/11/sunset-1024x683.jpg');">
Hero area
</div>How It Works
- The
large size is used for the background. - The
if skips the block when no image is found. esc_url() keeps the style attribute safe.
Frequently Asked Questions
An array with the image URL, width, height, and a flag if it is a resized copy. It returns false if the image is not found.
Index 0 is the URL, 1 is the width, 2 is the height, and 3 is the is_intermediate flag.
wp_get_attachment_image() returns an img tag. wp_get_attachment_image_src() returns an array of values.
Pass full as the second argument.
You get false, so check for it before you use the array.
Browse all post functions
Open the functions index for simple guides with examples and FAQs.
Post Functions Index →About the author
Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
9 people found this page helpful