Getting an image attachment URL in WordPress using wp_get_attachment_image_src

The WordPress wp_get_attachment_image_src function retrieves the URL of an image attachment. This can be useful for displaying images in different sizes or for customizing the display of images on a WordPress website.

By using this function, developers can easily access the URL of an image attachment and use it in their templates or custom functions. This can help in creating dynamic and responsive image displays on a website without having to manually specify the image URLs.

Parameters Accepted by wp_get_attachment_image_src Function

  • $attachment_id (int, required): Image attachment ID.
  • $size (string|int[], optional, default: ‘thumbnail’): Image size. Accepts any registered image size name, or an array of width and height values in pixels (in that order).
  • $icon (bool, optional, default: false): Whether the image should fall back to a mime type icon.

Value Returned by wp_get_attachment_image_src Function

The function returns an array of image data, or boolean false if no image is available. The array includes the following:

  • Image source URL
  • Image width in pixels
  • Image height in pixels
  • Whether the image is a resized image

Examples

How to get the URL of the full-size image attachment

$image_id = 123;
$image_url = wp_get_attachment_image_src( $image_id, 'full' );

This code snippet uses the wp_get_attachment_image_src function to retrieve the URL of the full-size image attachment with the ID of 123.

How to get the URL of a custom image size attachment

$image_id = 123;
$image_url = wp_get_attachment_image_src( $image_id, 'custom-size' );

This code snippet uses the wp_get_attachment_image_src function to retrieve the URL of a custom image size attachment with the ID of 123. The custom size is specified by the ‘custom-size’ parameter.

How to get the URL and dimensions of the image attachment

$image_id = 123;
$image_data = wp_get_attachment_image_src( $image_id, 'medium' );
$image_url = $image_data[0];
$image_width = $image_data[1];
$image_height = $image_data[2];

This code snippet uses the wp_get_attachment_image_src function to retrieve the URL, width, and height of the medium-size image attachment with the ID of 123. The function returns an array containing the image URL, width, and height, which are then assigned to variables for further use.

Conclusion

In conclusion, the wp_get_attachment_image_src function is a powerful tool for retrieving the URL and other information about an image attachment in WordPress. Its flexibility and ease of use make it a valuable asset for developers and designers working with WordPress media. By understanding how to use this function effectively, you can enhance the functionality and user experience of your WordPress website or application. Whether you are building a custom gallery, integrating images into a theme, or creating a custom image display, the wp_get_attachment_image_src function provides the necessary functionality to retrieve image data with ease.

Related WordPress Functions