On this page
wp_get_attachment_image_src( int $attachment_id, string|int[] $size = 'thumbnail', bool $icon = false ): array|false
Retrieves an image to represent an attachment.
Parameters
$attachment_idint Required-
Image attachment ID.
$sizestring|int[] Optional-
Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order). Default
'thumbnail'.Default:
'thumbnail' $iconbool Optional-
Whether the image should fall back to a mime type icon.
Default:
false
Return
array|false Array of image data, or boolean false if no image is available.
- string
Image source URL.
1intImage width in pixels.2intImage height in pixels.3boolWhether the image is a resized image.
Source
File: wp-includes/media.php. View all references
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 );
}
Hooks
- apply_filters( 'icon_dir',
string $path ) -
Filters the icon directory path.
- apply_filters( 'wp_get_attachment_image_src',
array|false $image ,int $attachment_id ,string|int[] $size ,bool $icon ) -
Filters the attachment image source result.
Related
Uses
| Uses | Description |
|---|---|
| wp_getimagesize() wp-includes/media.php | Allows PHP’s getimagesize() to be debuggable when necessary. |
| image_downsize() wp-includes/media.php | Scales an image to fit a particular size (such as ‘thumb’ or ‘medium’). |
| wp_mime_type_icon() wp-includes/post.php | Retrieves the icon for a MIME type or attachment. |
| wp_basename() wp-includes/formatting.php | i18n-friendly version of basename(). |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| WP_REST_Attachments_Controller::prepare_item_for_response() wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php | Prepares a single attachment output for response. |
| get_oembed_response_data_rich() wp-includes/embed.php | Filters the oEmbed response data to return an iframe embed code. |
| wp_get_attachment_image_srcset() wp-includes/media.php | Retrieves the value for an image attachment’s ‘srcset’ attribute. |
| wp_get_attachment_image_sizes() wp-includes/media.php | Retrieves the value for an image attachment’s ‘sizes’ attribute. |
| wp_get_attachment_image_url() wp-includes/media.php | Gets the URL of an image attachment. |
| wp_save_image() wp-admin/includes/image-edit.php | Saves image to post, along with enqueued changes in |
| edit_form_image_editor() wp-admin/includes/media.php | Displays the image and editor in the post editor |
| get_media_item() wp-admin/includes/media.php | Retrieves HTML form for modifying the image attachment. |
| Custom_Image_Header::step_2() wp-admin/includes/class-custom-image-header.php | Display second step of custom header image page. |
| Custom_Background::wp_set_background_image() wp-admin/includes/class-custom-background.php | |
| Custom_Background::handle_upload() wp-admin/includes/class-custom-background.php | Handles an Image upload for the background image. |
| wp_prepare_attachment_for_js() wp-includes/media.php | Prepares an attachment post object for JS, where it is expected to be JSON-encoded and fit into an Attachment model. |
| wp_playlist_shortcode() wp-includes/media.php | Builds the Playlist shortcode output. |
| wp_get_attachment_image() wp-includes/media.php | Gets an HTML img element representing an image attachment. |
Changelog
| Version | Description |
|---|---|
| 2.5.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src