How To Retrieve An Image Attachment’s Alt Text?

Posted by Prem Tiwari | Updated on

Consider looking at wp_prepare_attachment_for_js( $attachment ), where $attachment is the WP_Post object of the attachment itself. This is particularly useful (as the name implies), for sending the attachment image meta to a View via wp_send_ajax(), but that doesn’t mean you couldn’t use it for other purposes.

I like abstracting away from the _wp_attachment_image_alt post meta field, in case the method to retrieve the alt text ever changes (unlikely, but conceivable).

I do feel that there’s a case for a wp_get_attachment_image_alt() method however.

If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@] Your question and answer will appear on and help other developers.

Related Questions & Answers