Function(s) to get an attachments caption / title / alt / description.

  1. Luke Mlsna


    There are at least 8 functions that deal with manipulating attached images, but none (AFAIK) to get an attachment's caption, title, alt, or description (at least nothing clean and obvious).

    These aren't difficult to retrieve, and normally I wouldn't bother with a separate function to get them. However, because attachment objects use the same keys as post objects, it is not immediately clear where these 4 values are stored from looking at the key names, and the alt value is actually stored in post meta.

    I suggest separate functions to return/echo these values given an ID (like get_attachment_caption(), etc), or perhaps one function to return an associative array of values pertinent to the img element. I would think most theme developers would love something like this:

    function wp_get_attachment( $attachment_id ) {
    	$attachment = get_post( $attachment_id );
    	return array(
    		'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
    		'caption' => $attachment->post_excerpt,
    		'description' => $attachment->post_content,
    		'href' => get_permalink( $attachment->ID ),
    		'src' => $attachment->guid,
    		'title' => $attachment->post_title
    Posted: 7 years ago #
  2. Luke Mlsna

  3. mrwweb

    Are you looking for wp_get_attachment_metadata()?

    Posted: 7 years ago #
  4. Luke Mlsna


    No, that function provides the image meta data, like the exif data from the camera, not the wordpress meta data, like src, title, caption, description, etc.

    Posted: 7 years ago #

  5. Inactive

    Wow, thanks so much for this. I spent hours digging through the codex and random blogs trying to pluck these values given an attachment ID (or I guess really, a post ID). If you're in the loop, it gets pretty messy to do the equivalent of what your function does in the midst of other junk.

    For anyone finding this via google, you can dump bitacre's function in your theme's functions.php file and then call it as

    $attachment_meta = wp_get_attachment(your_attachment_id);

    And then either loop through the array values or simply reference by the key name of what you want (ie: caption, description, etc.):

    echo $attachment_meta['caption'];

    The above would echo the image's caption.

    Posted: 6 years ago #
  6. sheepysheep60

    This was great, thank you so much. Had a Custom field with a image URL in it, was able to grab the image id then get all of its data using this function.

    $img_id = get_attachment_id_from_src($image_url);
    $img_info =  wp_get_attachment( $img_id );
    echo $img_info['caption'];
    function get_attachment_id_from_src ($image_src) {
    	global $wpdb;
    	$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$image_src'";
    	$id = $wpdb->get_var($query);
    	return $id;
    Posted: 6 years ago #
  7. Marko Heijnen


    Currently I'm working on something simular but this not yet integrated. See https://github.com/markoheijnen/WP_Image.

    Posted: 5 years ago #
  8. aris00


    Hey Luke,

    I found this helpful. Images are posts, the excerpt is their caption.


    Posted: 5 years ago #
  9. Justin Busa

    The function wp_prepare_attachment_for_js($attachment_id) returns an array of everything you could ever want for an attachment. I found it useful for what I was trying to accomplish.

    See: http://codex.wordpress.org/Function_Reference/wp_prepare_attachment_for_js

    Posted: 5 years ago #
  10. lancemonotone

    @justinbusa, you are appreciated. I've been working in WP for over a decade and have never once stumbled upon this function. Thank you!

    It goes without saying that I absolutely love working in WP but I wish the core devs would clean up the function nomenclature for images. I always have to reach for a reference.

    Posted: 5 years ago #

RSS feed for this topic

Topic Closed

This topic has been closed to new replies.

  • Rating

    19 Votes
  • Status

    This idea has been implemented