Home WordPress Posts Functions wp_get_attachment_metadata() WordPress wp_get_attachment_metadata() Function 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().
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( $meta ) {
echo $meta['width'] . ' x ' . $meta['height'];
} Call What 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 wp_get_attachment_metadata( int $attachment_id = 0, bool $unfiltered = false ): array|false Parameters Parameter Description $attachment_idint. The attachment ID. Default is 0, the current post. $unfilteredbool. true skips filters. Default is false.
Return Value Return Value Description array The details. For images this has width, height, file, sizes, and image_meta. false If there is no metadata for the attachment.
WordPress Examples Three short examples. Each one shows the code and what it prints.
1. Get the original size 2. List the resized copies 3. Get the file path Example 1 — Get the original size Print the width and height of the original image.
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( $meta ) {
echo $meta['width'] . ' x ' . $meta['height'];
} How It Works The function returns an array of details. width and height are the size of the original file.The if makes sure there is data. Example 2 — List the resized copies Loop over the sizes key.
<?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";
}
} thumbnail: 150 x 150
medium: 300 x 200
large: 1024 x 683 How It Works sizes holds one entry for each resized copy.Each entry has a file name, width, height, and type. We print the name and the size. Example 3 — Get the file path The file key is the path inside the uploads folder.
<?php
$meta = wp_get_attachment_metadata( 15 );
if ( isset( $meta['file'] ) ) {
echo $meta['file'];
} How It Works file is a path relative to the uploads folder.isset() protects us when the key is missing.For the full URL use wp_get_attachment_url(). Frequently Asked Questions What does wp_get_attachment_metadata() return? An array of saved details about an attachment, such as width, height, file path, and resized copies.
What keys does an image have? Usually width, height, file, sizes, and image_meta.
What does it return if there is no data? It returns false.
How do I get the file URL? Use wp_get_attachment_url().
How do I get one resized image? 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 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.
11 people found this page helpful
Helpful Share Copy link Suggestion