Wordpress - How to get all featured image sizes and their URLs?
I don't remember a function that will do precisely that, but it is easily achieved with API overall:
- Retrieve attachment ID with
get_post_thumbnail_id()
- Retrieve available sizes with
get_intermediate_image_sizes()
- For each size use
wp_get_attachment_image_src()
, which gives precisely data you need (URL and dimensions).
After you get it's ID ( get_post_thumbnail_id($YOUR_POST_ID) ) you need to know the uploads base url and the attachment meta data.
Uploads base url can be found in an array returned by wp_upload_dir().
Attachment meta data can be retrieved by wp_get_attachment_metadata().
Here is a handy function I wrote that prepares the url data for any image attachment, not just featured.
function prepareImageData( $attachment_id ){
$uploads_baseurl = wp_upload_dir()['baseurl'];
$prepared = [];
$data = wp_get_attachment_metadata($attachment_id);
$prepared = [
'mime_type' => get_post_mime_type($attachment_id),
'url' => $uploads_baseurl.'/'.$data['file'],
'sizes' => [],
];
foreach( $data['sizes'] as $size => $sizeInfo ){
$prepared['sizes'][$size] = [
'url' => $uploads_baseurl.'/'.$sizeInfo['file'],
];
}
return $prepared;
}