WordPress.org

Make WordPress Core

Opened 3 years ago

Last modified 3 months ago

#30180 accepted enhancement

wp_get_attachment_image_src does not return alt or meta

Reported by: joedolson Owned by: joedolson
Milestone: 4.8 Priority: normal
Severity: normal Version:
Component: Media Keywords: needs-testing needs-patch
Focuses: accessibility Cc:

Description

In practical use,wp_get_attachment_image_src is very useful for displaying an attached image - but requires a separate call to get any other image meta or the alt attribute.

In addition to making any application that needs alt attr or meta data for display simpler, having the alt attribute present in this function increases the likelihood that developers will use the alt attribute in their ultimate output.

Attachments (2)

30180.patch (632 bytes) - added by joedolson 3 years ago.
Adds image alt and meta data to array output
30180-unit-test.patch (825 bytes) - added by collinsinternet 2 years ago.
Unit test to test return of alt.

Download all attachments as: .zip

Change History (18)

@joedolson
3 years ago

Adds image alt and meta data to array output

#1 @collinsinternet
2 years ago

  • Keywords reporter-feedback needs-testing added

This doesn't work since image_downsize() is returned prior to your additions (at least in my tests). Also you will need to update the PHPDoc Block @return value to reflect what is being returned.

@collinsinternet
2 years ago

Unit test to test return of alt.

#2 @joedolson
2 years ago

Yes, image_downsize will need patching as well.

#3 @chriscct7
17 months ago

  • Keywords needs-patch added; has-patch reporter-feedback removed

#4 @joemcgill
17 months ago

It may be better to create a new function that returns these attributes, rather than adding more logic to image_downsize() since image_downsize() is already pretty slow and is used in the logic chain of many other image functions, like wp_get_attachment_image() and get_post_thumbnail().

This ticket was mentioned in Slack in #accessibility by rianrietveld. View the logs.


12 months ago

#6 @joedolson
12 months ago

  • Milestone changed from Awaiting Review to 4.6
  • Owner set to joedolson
  • Status changed from new to accepted

This needs some resolution; which may or may not be adapting this function. Will work on this for 4.6.

This ticket was mentioned in Slack in #core-images by joemcgill. View the logs.


12 months ago

#8 @joemcgill
12 months ago

As discussed with @joedolson earlier this week, while I am 100% behind the intent of this change, I don't think that adding additional attributes to wp_get_attachment_image_src() is the right approach.

Instead, I think we should look into creating a new function that reuses the internals of wp_get_attachement_image() but returns the attributes as an array of values. Possibly something like wp_get_attachment_image_data() or wp_get_attachment_image_atts(). We can then make wp_get_attachment_image() a wrapper function that converts the output to HTML.

Additionally, it might be helpful to have a helper function like wp_get_attachment_image_alt() that can be used as a standard way of generating an alt attribute when someone is rolling their own image markup.

#9 follow-up: @joedolson
12 months ago

I think this is a great plan. A few questions come to mind:

Which attributes should we return?

Is the purpose of the wp_get_attachment_image_data() function to return all data about the attachment image, or only what's required to generate the img element? E.g., should we return the caption? Should we return the internal image title (as opposed to the title attribute)? Should we return any custom meta fields available for the attachment?

I'm inclined to think that it should only be the fields needed for the img element, but we should probably decide that before building a patch.

#10 in reply to: ↑ 9 @joemcgill
11 months ago

Replying to joedolson:

I think this is a great plan. A few questions come to mind:

Which attributes should we return?
...
I'm inclined to think that it should only be the fields needed for the img element...

Good question. I could see use cases for both, to be honest. A function that only returns an array of attribute values that can be used in img markup could keep the function lighter, but I could also see cases where knowing additional metadata (like which sizes are registered in the database with that image) could be valuable to JS front-ends that are using the information to do more complex calculations based on the result of API calls.

However, a more advanced use case might be better addressed by a proper WP_Image API rather than speculating and trying to do too much here, so a function that returns attributes for the img element is probably a good starting point. With that being the case, I would suggest we name the function wp_get_attachment_image_attributes() (to match the name of the enclosed filter that would need to be maintained).

#11 follow-up: @joemcgill
10 months ago

  • Milestone changed from 4.6 to Future Release
  • Type changed from defect (bug) to enhancement

Now that we have identified a path forward, this is more of an enhancement than a bug, so I'm going to move this out of the 4.6 milestone now that we're in beta. Let's see what can be done for the next release.

This ticket was mentioned in Slack in #accessibility by afercia. View the logs.


6 months ago

#13 @afercia
6 months ago

  • Milestone changed from Future Release to 4.8

#14 in reply to: ↑ 11 @bhargavbhandari90
4 months ago

Replying to joemcgill:

Now that we have identified a path forward, this is more of an enhancement than a bug, so I'm going to move this out of the 4.6 milestone now that we're in beta. Let's see what can be done for the next release.

So we can make function like get_post_meta for image attributes.

This ticket was mentioned in Slack in #core-media by joemcgill. View the logs.


3 months ago

This ticket was mentioned in Slack in #core-media by joedolson. View the logs.


3 months ago

Note: See TracTickets for help on using tickets.