WordPress Media Attachment Helpers: wp_get_attachment_image, URL, Metadata, and MIME Types

Attachment helpers: which function returns what

WordPress has a whole family of functions for attachments, and they differ in what they return: a complete <img> tag, the URL of the original file, the URL and size of one image size, the metadata of all sizes. Picking the right one saves code and gives responsive, accessible markup for free.

Problem: A post has a gallery field with attachment IDs. Each image must be shown responsively with a link to the original file, and the code should not build image tags by hand.

Solution: Use wp_get_attachment_image() for the tag, wp_get_attachment_url() for the original file, wp_get_attachment_image_src() when you need the URL and size of one image size, and wp_get_attachment_metadata() for all sizes.

The image tag

<?php
$ids = (array) get_post_meta( get_the_ID(), 'ha_gallery_ids', true );

foreach ( array_filter( array_map( 'absint', $ids ) ) as $id ) {
    printf(
        '<figure class="gallery__item">%s<figcaption><a href="%s">%s</a></figcaption></figure>',
        wp_get_attachment_image( $id, 'large', false, array( 'class' => 'gallery__image' ) ),
        esc_url( wp_get_attachment_url( $id ) ),
        esc_html__( 'Original file', 'textdomain' )
    );
}

wp_get_attachment_image() adds src, srcset, sizes, width, height, the alt text from the media library and, since WordPress 5.5, loading="lazy". Pass your own attributes in the fourth argument; there is no need to read the alt text yourself.

One size: URL, width and height

<?php
$src = wp_get_attachment_image_src( $id, 'thumbnail' ); // array( url, width, height, is_intermediate ) or false

if ( $src ) {
    list( $url, $width, $height ) = $src;
}

$url_only = wp_get_attachment_image_url( $id, 'medium' ); // just the URL

Use these for places where a tag does not fit: a CSS background, an Open Graph tag, a JSON response.

Metadata and type

<?php
$meta = wp_get_attachment_metadata( $id );
// $meta['width'], $meta['height']: the original size
// $meta['file']: the path relative to the uploads folder
// $meta['sizes']: all generated sizes with file, width, height and mime-type

$mime     = get_post_mime_type( $id );         // 'image/jpeg', 'application/pdf'...
$is_image = wp_attachment_is_image( $id );
$path     = get_attached_file( $id );           // absolute path on the server
$parent   = wp_get_post_parent_id( $id );       // the post it was uploaded to, 0 if none

From a URL back to the ID

<?php
$id = attachment_url_to_postid( 'https://example.com/wp-content/uploads/2020/12/photo.jpg' );

It finds the original file only, not a resized copy like photo-300×200.jpg, and since WordPress 5.3 large uploads are stored as photo-scaled.jpg, so the URL you have may not be the one in the database. Each call is a database query, so do not use it in loops; store attachment IDs instead of URLs wherever you can. Finding the alt text and title by URL is shown in getting the image alt and title by URL.

Featured images and querying the media library are covered in setting and getting featured images in code and querying the media library with WP_Query.

NOTE: wp_get_attachment_url() returns the original upload, which for photos from a camera can be several megabytes. Use it for download links, and a named size from wp_get_attachment_image() for anything shown on the page.