Get Image Alt, Caption, Description and More From Attachment ID in WordPress
Whilst working on a WordPress site recently I had a scenario where I had an image attachment ID and needed to get the alt, plus other information, about the image.
Oddly, I found it very difficult to obtain this information. There are lots of functions available in WordPress to get the image src, sizes etc, but I could not find anyway to get this additional information.
Finally I stumbled upon a function called wp_prepare_attachment_for_js(). You can use the function like so:
$attachment_data = wp_prepare_attachment_for_js( $attachment_id );
By passing in an attachment ID to this function you can get a whole heap of information about the image.