wp_get_attachment_thumb_file

Definition:
function wp_get_attachment_thumb_file( $post_id = 0 ) {}

Retrieve thumbnail for an attachment.

Parameters

  • int $post_id: Attachment ID.

Return values

returns:False on failure. Thumbnail file path on success.

Defined filters

  • wp_get_attachment_thumb_file
    apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID )

Source code

function wp_get_attachment_thumb_file( $post_id = 0 ) {

	$post_id = (int) $post_id;

	if ( !$post =& get_post( $post_id ) )

		return false;

	if ( !is_array( $imagedata = wp_get_attachment_metadata( $post->ID ) ) )

		return false;



	$file = get_attached_file( $post->ID );



	if ( !empty($imagedata['thumb']) && ($thumbfile = str_replace(basename($file), $imagedata['thumb'], $file)) && file_exists($thumbfile) )

		return apply_filters( 'wp_get_attachment_thumb_file', $thumbfile, $post->ID );

	return false;

}

3689

wp_get_attachment_link

Definition:
function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {}

Retrieve an attachment page link using an image or icon, if possible.

Parameters

  • int $id: Optional. Post ID.
  • string $size: Optional, default is ‘thumbnail’. Size of image, either array or string.
  • bool $permalink: Optional, default is false. Whether to add permalink to image.
  • bool $icon: Optional, default is false. Whether to include icon.
  • string $text: Optional, default is false. If string, then will be link text.

Return values

returns:HTML content.

Defined filters

  • wp_get_attachment_link
    apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text )

Source code

function wp_get_attachment_link( $id = 0, $size = 'thumbnail', $permalink = false, $icon = false, $text = false ) {

	$id = intval( $id );

	$_post = & get_post( $id );



	if ( empty( $_post ) || ( 'attachment' != $_post->post_type ) || ! $url = wp_get_attachment_url( $_post->ID ) )

		return __( 'Missing Attachment' );



	if ( $permalink )

		$url = get_attachment_link( $_post->ID );



	$post_title = esc_attr( $_post->post_title );



	if ( $text )

		$link_text = esc_attr( $text );

	elseif ( $size && 'none' != $size )

		$link_text = wp_get_attachment_image( $id, $size, $icon );

	else

		$link_text = '';



	if ( trim( $link_text ) == '' )

		$link_text = $_post->post_title;



	return apply_filters( 'wp_get_attachment_link', "<a href='$url' title='$post_title'>$link_text</a>", $id, $size, $permalink, $icon, $text );

}

3685

wp_get_attachment_image_src

Definition:
function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {}

Retrieve an image to represent an attachment.
A mime icon for files, thumbnail or intermediate size for images.

Parameters

  • int $attachment_id: Image attachment ID.
  • string $size: Optional, default is ‘thumbnail’.
  • bool $icon: Optional, default is false. Whether it is an icon.

Return values

returns:Returns an array (url, width, height), or false, if no image is available.

Defined filters

  • icon_dir
    apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' )

Source code

function wp_get_attachment_image_src($attachment_id, $size='thumbnail', $icon = false) {



	// get a thumbnail or intermediate image if there is one

	if ( $image = image_downsize($attachment_id, $size) )

		return $image;



	$src = false;



	if ( $icon && $src = wp_mime_type_icon($attachment_id) ) {

		$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/crystal' );

		$src_file = $icon_dir . '/' . wp_basename($src);

		@list($width, $height) = getimagesize($src_file);

	}

	if ( $src && $width && $height )

		return array( $src, $width, $height );

	return false;

}

3683

wp_get_attachment_image

Definition:
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {}

Get an HTML img element representing an image attachment
While $size will accept an array, it is better to register a size with add_image_size() so that a cropped version is generated. It’s much more efficient than having to find the closest-sized image and then having the browser scale down the image.

Parameters

  • int $attachment_id: Image attachment ID.
  • string $size: Optional, default is ‘thumbnail’.
  • bool $icon: Optional, default is false. Whether it is an icon.
  • $attr

Return values

returns:HTML img element or empty string on failure.

Defined filters

  • wp_get_attachment_image_attributes
    apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment )

Source code

function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {



	$html = '';

	$image = wp_get_attachment_image_src($attachment_id, $size, $icon);

	if ( $image ) {

		list($src, $width, $height) = $image;

		$hwstring = image_hwstring($width, $height);

		if ( is_array($size) )

			$size = join('x', $size);

		$attachment =& get_post($attachment_id);

		$default_attr = array(

			'src'	=> $src,

			'class'	=> "attachment-$size",

			'alt'	=> trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first

			'title'	=> trim(strip_tags( $attachment->post_title )),

		);

		if ( empty($default_attr['alt']) )

			$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption

		if ( empty($default_attr['alt']) )

			$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title



		$attr = wp_parse_args($attr, $default_attr);

		$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );

		$attr = array_map( 'esc_attr', $attr );

		$html = rtrim("<img $hwstring");

		foreach ( $attr as $name => $value ) {

			$html .= " $name=" . '"' . $value . '"';

		}

		$html .= ' />';

	}



	return $html;

}

3681