Get Image Alt, Caption, Description and More From Attachment ID in WordPress

December 1st, 2015 - Posted by Steve Marks to PHP, Web Development.

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.

The Solution

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.

This entry was posted on Tuesday, December 1st, 2015 at 9:59 am by +Steve Marks and is filed under PHP, Web Development. You can follow any responses to this entry through the RSS 2.0 feed.
Comments...

Fear not, we won't publish this

Comments (0)

No comments have been left yet. Be the first