wp_get_attachment_image_src()WP 2.5.0

Gets an array of data for the specified image: URL, width, height of the attachment image.

Note: use the first (zero) element of the array for the src attribute.

Use wp_get_attachment_image_url() when you need to get ready attachment URL.

1 time — 0.003252 sec (very slow) | 50000 times — 12.29 sec (slow) | PHP 7.1.2, WP 4.7.5
Hooks from the function

Return

Array|false. An array containing the following data:

[0] => url
[1] => width
[2] => height
[3] => is_intermediate

Usage

wp_get_attachment_image_src( $attachment_id, $size, $icon );
$attachment_id(number) (required)
The ID of the attachment image whose data you want to retrieve.
$size(string/array)

The size of the image whose data you want to retrieve. In this parameter you can specify the size provided in WordPress thumbnail, medium, large, full or any other registered size.

You can also specify a size as an array in the form of two elements (width, height): array( 32, 32 ). In this case the most suitable size will be chosen and then the image will be shrunk/stretched to that size.

Since version 2.5 this parameter does not affect the size of media icons (icons for files), they are always shown in their original size.

Default: 'thumbnail'

$icon(true/false)
Set to true (1) to show media icons for non-image attachments (.zip, .rar, etc.)
Default: false

Examples

0

#1 Default usage

Let's output the html code of the attachment image with ID=8:

<?php
$attachment_id = 8;

$image_attributes = wp_get_attachment_image_src( $attachment_id );
// array returned
?>

<img src="<?php echo $image_attributes[0] ?>" width="<?php echo $image_attributes[1] ?>" height="<?php echo $image_attributes[2] ?>">
0

#2 Changing the icon directory

In WordPress, special icons are used to show images for attached files (not pictures). For such display is responsible parameter $icon. So, for image attachments it's image is displayed, but for other files is displayed an icon corresponding to its type. For example, for a .mp3 file will be shown the image audio.jpg from the wp-includes/images/crystal/ folder.

This example shows how to change the section where the media icons will come from. Let's change the default directory to the theme folder: wp-content/themes/yourtheme/images (of course, this folder should contain images for different file types).

add_filter( 'icon_dir', 'my_theme_icon_dyrectory' );
function my_theme_icon_dyrectory( $icon_dir ) {
   return TEMPLATEPATH . '/images';
}

add_filter( 'icon_dir_uri', 'my_theme_icon_uri' );
function my_theme_icon_uri( $icon_dir ) {
   return get_bloginfo( 'stylesheet_directory' ) . '/images';
}

This code can be placed to the functions.php theme file

Changelog

Since 2.5.0 Introduced.

wp_get_attachment_image_src() code WP 6.4.3

function wp_get_attachment_image_src( $attachment_id, $size = 'thumbnail', $icon = false ) {
	// Get a thumbnail or intermediate image if there is one.
	$image = image_downsize( $attachment_id, $size );
	if ( ! $image ) {
		$src = false;

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

			if ( $src ) {
				/** This filter is documented in wp-includes/post.php */
				$icon_dir = apply_filters( 'icon_dir', ABSPATH . WPINC . '/images/media' );

				$src_file               = $icon_dir . '/' . wp_basename( $src );
				list( $width, $height ) = wp_getimagesize( $src_file );
			}
		}

		if ( $src && $width && $height ) {
			$image = array( $src, $width, $height, false );
		}
	}
	/**
	 * Filters the attachment image source result.
	 *
	 * @since 4.3.0
	 *
	 * @param array|false  $image         {
	 *     Array of image data, or boolean false if no image is available.
	 *
	 *     @type string $0 Image source URL.
	 *     @type int    $1 Image width in pixels.
	 *     @type int    $2 Image height in pixels.
	 *     @type bool   $3 Whether the image is a resized image.
	 * }
	 * @param int          $attachment_id Image attachment ID.
	 * @param string|int[] $size          Requested image size. Can be any registered image size name, or
	 *                                    an array of width and height values in pixels (in that order).
	 * @param bool         $icon          Whether the image should be treated as an icon.
	 */
	return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}