[wp-trac] [WordPress Trac] #21851: new function: wp_get_attachment_image_atts()
WordPress Trac
wp-trac at lists.automattic.com
Sat Sep 8 23:01:13 UTC 2012
#21851: new function: wp_get_attachment_image_atts()
-------------------------+-----------------------------
Reporter: bitacre | Owner: bitacre
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: Themes | Version: trunk
Severity: minor | Keywords:
-------------------------+-----------------------------
wp_get_attachment_image() returns the full HTML for displaying an image
given id, size, etc; and wp_get_attachment_image_src() returns the image
location; and wp_get_attachment_metadata() returns file meta data (like
exif); but afaik, there is no function that returns the attributes of a
specific attachment given attachment_id (things like title, src, alt,
caption, description.
These attributes are all available using various other functions, but are
stored in various, non-obvious places in the $post and post meta. I
suggest a new function to return this data either as an associative array
or stdClass. Perhaps something like the following:
{{{
function wp_get_attachment_atts( $attachment_id='' ) {
if( empty( $attachment_id ) ) $attachment_id = get_the_ID(); //
use current id if none
if( !wp_attachment_is_image( $attachment_id ) ) return false; //
check if attachment
$attachment = get_post( $attachment_id ); // get attachment
if( !$attachment ) return false;
else 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 ),
'parent_id' => $attachment->post_parent,
'src' => $attachment->guid,
'title' => $attachment->post_title );
}
}}}
I apologize if a similar function already exists, but I'm not aware of it.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/21851>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list