Huy Hòa
  • Home
  • Code Learn
    • WordPress
    • Woocommerce
    • Joomla
    • PHP
    • SEO – Marketing
  • Vietnam
  • Healthcare
    • Womens Health
  • Beauty
  • Lifehack
  • Free Tools
    • Dog Pregnancy Calculator
    • BMI Calculator
    • Upside Down Text
    • Vietnamese Calendar
    • Vietnamese Typing
  • Contact Us
Trending
  • Tet 2026: Everything You Need to Know About the Vietnamese Lunar New Year
  • PHP Foreach Loop Tutorial: Syntax, Examples, and Best Practices
  • Rash vs. Hives: How to Identify and Treat Them Naturally
  • How to Get Rid of a Pimple Inside Your Nose: A Comprehensive Guide
  • Ear Hygiene: How to Clean Your Ears the Right Way
Tuesday, May 20
Huy Hòa
  • Home
  • Code Learn
    • WordPress
    • Woocommerce
    • Joomla
    • PHP
    • SEO – Marketing
  • Vietnam
  • Healthcare
    • Womens Health
  • Beauty
  • Lifehack
  • Free Tools
    • Dog Pregnancy Calculator
    • BMI Calculator
    • Upside Down Text
    • Vietnamese Calendar
    • Vietnamese Typing
  • Contact Us
Huy Hòa
Home»WordPress»wp_get_attachment_image_src() – a WordPress Function to Returns Array of Image Data

wp_get_attachment_image_src() – a WordPress Function to Returns Array of Image Data

Facebook Twitter Pinterest LinkedIn Tumblr Email
wp_get_attachment_image_src() - a WordPress Function
wp_get_attachment_image_src() - a WordPress Function

wp_get_attachment_image_src() is a WordPress Function retrieve an image to represent an attachment. In practical use wp_get_attachment_image_src is very useful for displaying an attached image if you don’t need image meta or the alt attribute.

Table of Contents

  • 1 Source:
  • 2 Usage:
    • 2.1 Parameters
  • 3 Example:

Source:

File: wp-includes/media.php

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 );
        }
    }
   return apply_filters( 'wp_get_attachment_image_src', $image, $attachment_id, $size, $icon );
}

Usage:

wp_get_attachment_image_src( $attachment_id = 18 );

Parameters

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

By default, wp_get_attachment_image_src return an array of image with 4 values url, width, height, is_intermediateIf there is no image, the function returns false.

Example:

Get URL, width, the height of image with attachment id 18:

$img_attributes = wp_get_attachment_image_src( $attachment_id = 18 );
if ( $img_attributes ) : ?>
    <img src="<?php echo $img_attributes[0]; ?>" width="<?php echo $img_attributes[1]; ?>" height="<?php echo $img_attributes[2]; ?>" />
<?php endif; ?>

To get the original size URL of an image in the function wp_get_attachment_image_src use:

$img_atts = wp_get_attachment_image_src(18, 'full'); // 18 is ID of attachment, NOT post ID
<img src="<?php echo $img_atts[0]; ?>"

Size option:  If you want to get the URL of an attachment in custom size, you can declare $size according to array(width, height)

The example below used wp_get_attachment_image_src to takes the thumbnail size image and the custom size is 640x400

$thumbnail_url_custom = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), array('640','400'), true ); // Custom size 640x400
$thumbnail_url_thumb = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail', true ); // Thumbnail size

$thumbnail_url_thumb = $thumbnail_url_thumb[0]; // Somthing like: https://huyhoa.net/wp-content/uploads/2021/07/how-to-get-post-id-150x150.jpg

$thumbnail_url_custom = $thumbnail_url_custom[0];  // Somthing like: https://huyhoa.net/wp-content/uploads/2021/07/how-to-get-post-id-640x400.jpg

Please note that if the thumbnail size 640×400 does not exist, wp_get_attachment_image_src will not working and return false.

In some cases, the array $size will not working in wp_get_attachment_image_src function. The reason is that $size does not exist, so you have to add that $size support by use adds image size function in your functions.php file like this.

add_action( 'after_setup_theme', 'huyhoa_theme_setup' );
function huyhoa_theme_setup() {
    add_image_size( 'category-thumb', 180 ); // 300 pixels wide (and unlimited height)
    add_image_size( 'homepage-thumb', 120, 120, true ); // (cropped to 120px x 120px)
}

Then you can use this code to get the image URL with the defined size

wp_get_attachment_image_src( $thumbnail_id, 'category-thumb', '', true );

Another example of getting the attachment URL for the medium-sized image

 wp_get_attachment_image_src( $thumbnail_id, 'medium')[0];

Alt text: wp_get_attachment_image_src function will not return an image attachment’s Alt text. If you want to get alt text, you should use wp_get_attachment_image() a function instead.

Bonus: In case you want to get an attachment ID from an image URL, please use this function:

function get_attachment_id_from_src ($src) {
    global $wpdb;
    $reg = "/-[0-9]+x[0-9]+?.(jpg|jpeg|png|gif)$/i";
    $src1 = preg_replace($reg,'',$src);
    if($src1 != $src){
    $ext = pathinfo($src, PATHINFO_EXTENSION);
    $src = $src1 . '.' .$ext;
}
$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$src'";
$id = $wpdb->get_var($query);
return $id;
}

References:

4.7/5 - (996 votes)
Wordpress
Share. Facebook Twitter Pinterest LinkedIn Tumblr Email
Previous ArticleComprehensive Guide to Cancer: Types, Symptoms, Prevention & Treatment
Next Article How to Lose Fat Without Losing Muscle: 7 Tried-and-True Methods
Huy Hoa
  • Website

In Case You Missed It

PHP Foreach Loop Tutorial: Syntax, Examples, and Best Practices
A Guide to get_post_meta() Function in WordPress
Master WP Cron: A Comprehensive Guide to Scheduling, Troubleshooting, and Optimization
The 41+ best free and premium WooCommerce themes
30+ Useful Custom WooCommerce Functions for Your WordPress Site
Optimize performance and security for WordPress using .htaccess
How to get post data in WordPress?
How to Use Woocommerce_form_field() Function with 16 Examples
5 examples of getting Post by ID & 11 Ways to Get Post ID in WordPress

Leave A Reply Cancel Reply

Search
Wordpress Themes & Plugins

A Guide to get_post_meta() Function in WordPress

If you want to add some extra information or functionality to your WordPress posts, you might need to use custom fields and metadata. Custom fields…

wp_get_attachment_image_src() – a WordPress Function to Returns Array of Image Data

The 41+ best free and premium WooCommerce themes

How to Use Woocommerce_form_field() Function with 16 Examples

How to get post data in WordPress?

Master WP Cron: A Comprehensive Guide to Scheduling, Troubleshooting, and Optimization

How to Fix PhpMyAdmin Error Incorrect Format Parameter

Trending
Vietnam

Tet 2026: Everything You Need to Know About the Vietnamese Lunar New Year

Tet, the Vietnamese Lunar New Year, is the most important and widely celebrated holiday in…

PHP Foreach Loop Tutorial: Syntax, Examples, and Best Practices

Rash vs. Hives: How to Identify and Treat Them Naturally

How to Get Rid of a Pimple Inside Your Nose: A Comprehensive Guide

Ear Hygiene: How to Clean Your Ears the Right Way

Triangle Face Shapes Guide: Hairstyles, Glasses & Makeup Tips

Vietnamese New Year Traditions

© 2025 All Rights Reserved  by  HuyHoa.Net. DMCA.com Protection Status .
Hosted by Dreamhost. Follow us on  Google News.
  • About Us
  • Privacy Policy
  • Cookie Policy
  • Contact Us

Type above and press Enter to search. Press Esc to cancel.