How To Retrieve An Image Attachment’s Alt Text?

I recently did some research for a client project recently so lo-and-behold I get to use it here!

After the text you’ll see a categorized list of most (all?) of the image handling functions from within WordPress 3.0.1 (I grouped them in some semblance of order but don’t put too much credence in my categorization.)

Anyway, answering what (I think) you need instead of what you asked for (okay, I’ll answer that too, at the end) I think what you need is the wp_get_attachment_image() function which will return an HTML string containing these attributes:

  • 'src',
  • 'class',
  • 'alt' and
  • 'title'.

WordPress 3.0 Image Handling Functions

So here are WordPress’ image handling functions for your and other’s reference (jump below for the answer to your exact question):

Image Support/Thumbnails

Attachment

MIME Types

Uploads

Filesystem

HTML

Low Level Image Handling:


As promised the Image’s 'alt' text is stored as a string in wp_postmeta with the meta_key of '_wp_attachment_image_alt'.

As you probably already know you can load it with a simple get_post_meta() like so:

$alt_text = get_post_meta($post->ID, '_wp_attachment_image_alt', true);

Leave a Comment