WordPress wp_get_attachment_metadata() Function

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

Definition

In WordPress, wp_get_attachment_metadata() returns the saved details of a file in the Media Library. For images this includes the width, height, file path, and a list of all resized copies.

Use it when you need more detail than just a URL.

Syntax

A few ways to call wp_get_attachment_metadata().

WordPress
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( $meta ) {
  echo $meta['width'] . ' x ' . $meta['height'];
}
CallWhat it does
wp_get_attachment_metadata( 15 )Get the details of attachment 15.
wp_get_attachment_metadata()Use the current attachment.
wp_get_attachment_metadata( 15, true )Skip filters and get the saved data.

It returns an array for images, and may return other data for audio and video.

Full declaration
WordPress
wp_get_attachment_metadata( int $attachment_id = 0, bool $unfiltered = false ): array|false

Parameters

ParameterDescription
$attachment_idint. The attachment ID. Default is 0, the current post.
$unfilteredbool. true skips filters. Default is false.

Return Value

Return ValueDescription
arrayThe details. For images this has width, height, file, sizes, and image_meta.
falseIf there is no metadata for the attachment.

WordPress Examples

Three short examples. Each one shows the code and what it prints.

Example 1 — Get the original size

Print the width and height of the original image.

WordPress
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( $meta ) {
  echo $meta['width'] . ' x ' . $meta['height'];
}

How It Works

  1. The function returns an array of details.
  2. width and height are the size of the original file.
  3. The if makes sure there is data.

Example 2 — List the resized copies

Loop over the sizes key.

WordPress
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( ! empty( $meta['sizes'] ) ) {
  foreach ( $meta['sizes'] as $name => $size ) {
    echo $name . ': ' . $size['width'] . ' x ' . $size['height'] . "\n";
  }
}

How It Works

  1. sizes holds one entry for each resized copy.
  2. Each entry has a file name, width, height, and type.
  3. We print the name and the size.

Example 3 — Get the file path

The file key is the path inside the uploads folder.

WordPress
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( isset( $meta['file'] ) ) {
  echo $meta['file'];
}

How It Works

  1. file is a path relative to the uploads folder.
  2. isset() protects us when the key is missing.
  3. For the full URL use wp_get_attachment_url().

Frequently Asked Questions

An array of saved details about an attachment, such as width, height, file path, and resized copies.
Usually width, height, file, sizes, and image_meta.
It returns false.
Use wp_get_attachment_url().
Use wp_get_attachment_image_src() with a size name.

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.

11 people found this page helpful