WordPress wp_get_attachment_image_src() Function

Beginner
6 min read
Updated: Nov 2026
3 examples
Returns an array

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().

WordPress
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
  echo esc_url( $image[0] );
}
CallWhat 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
WordPress
wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): array|false

Parameters

ParameterDescription
$attachment_idint. The ID of the image in the Media Library.
$sizestring|int[]. A size name or an array of width and height. Default is thumbnail.
$iconbool. true uses a media icon when the file is not an image. Default is false.

Return Value

Return ValueDescription
arrayAn array: [0] URL, [1] width, [2] height, [3] is_intermediate.
falseIf 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.

WordPress
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
  echo esc_url( $image[0] );
}

How It Works

  1. The function returns an array.
  2. $image[0] is the URL.
  3. esc_url() makes it safe to print.

Example 2 — Get the width and height

Use the other values of the array.

WordPress
<?php
$image = wp_get_attachment_image_src( 15, 'medium' );
if ( $image ) {
  list( $url, $width, $height ) = $image;
  echo $width . ' x ' . $height;
}

How It Works

  1. list() splits the array into three variables.
  2. The numbers are the size of the chosen image.
  3. We print them as 300 x 200.

Example 3 — Use it as a CSS background

Put the URL in an inline style.

WordPress
<?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; ?>

How It Works

  1. The large size is used for the background.
  2. The if skips the block when no image is found.
  3. 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

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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