0

When I use Advanced Custom Fields I can return the full image array of an upload. Is there a native WordPress way to get the full image array for any image in the Media Library by image ID? I can find ways to get the alt text and a specific image size, but not the array itself...

1

The "full array" is constructed by Advanced Custom Fields from various sources. It's not a format that occurs natively in WordPress.

If you want to output an image tag for an image with all the correct attributes, just use the ID with wp_get_attachment_image(). That returns an HTML <img> tag with the src, width, height, alt, srcset, sizes and class all populated. You can add or change attributes to the tag using the 4th argument:

echo wp_get_attachment_image(
    $attachment_id,
    'large',
    false
    [
        'class'          => 'custom-class',
        'data-attribute' => 'custom-attribute-value',
    ]
);

To be honest, if your goal is to ultimately get an <img> tag, then even with ACF you should return an ID and use this function.

| improve this answer | |
  • I'm marking your answer as correct as thanks for confirming there's no native function... I completely disagree about using that function instead of ACF no matter what though: ACF's array can be super useful - that's why they created it! Ultimately I created my own version of their array to use for native WordPress, which suits my purposes. – JohnG May 7 at 10:11
  • What are your purposes? – Jacob Peattie May 7 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.