Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#21851 closed enhancement (duplicate)

new function: wp_get_attachment_image_atts()

Reported by: bitacre's profile bitacre Owned by: bitacre's profile bitacre
Milestone: Priority: normal
Severity: minor Version:
Component: Themes Keywords:
Focuses: Cc:

Description

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.

Change History (1)

#1 @ocean90
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed
  • Version trunk deleted
Note: See TracTickets for help on using tickets.