How can I get full attachment url from wp_get_attachment_metadata?

I want to get all sizes of an attachment. I can use wp_get_attachment_metadata to do this. But it only return filenames, not file urls.

How can I do this?

This is a workaround, but doesn’t take stuff such as CDNs into account:

$attachment_id = get_term_meta( $term->term_id, 'thumbnail_id', true);

$image = wp_get_attachment_metadata($attachment_id);
$image['id'] = $attachment_id;


foreach($image['sizes'] as $key => $image_size) {
   $image['sizes'][$key]['url'] = wp_upload_dir()['baseurl'] . "https://wordpress.stackexchange.com/" . dirname($image['file']) . "https://wordpress.stackexchange.com/" . $image_size['file'];
}

1 Answer
1

Once you already have the attachment ID, you can get the full path with get_attached_file:

$image_path = get_attached_file( $attachment_id );

Leave a Comment