WordPress.org

Make WordPress Core

Opened 5 months ago

Last modified 3 months ago

#46124 accepted enhancement

Allow setting custom image alt text in custom header image

Reported by: webmandesign Owned by: audrasjb
Milestone: 5.3 Priority: normal
Severity: normal Version: 5.0.3
Component: Media Keywords: has-patch needs-refresh 2nd-opinion
Focuses: accessibility Cc:

Description

Currently when using custom header feature and displaying an image in custom header, the get_header_image_tag() function sets the custom header image alt text to get_bloginfo( 'name' ) only.

It is not possible to override the alt text by setting a custom alt text for the image in Media Library.

To improve accessibility, this should be possible.

Attachments (2)

ticket46124.diff (2.0 KB) - added by webmandesign 5 months ago.
Patch fixing ticket #46124
46124.patch (596 bytes) - added by mukesh27 5 months ago.
Updated patch.

Download all attachments as: .zip

Change History (17)

@webmandesign
5 months ago

Patch fixing ticket #46124

#1 @webmandesign
5 months ago

  • Keywords has-patch added

@mukesh27
5 months ago

Updated patch.

#2 @webmandesign
5 months ago

Hi @mukesh27,

Thank you for the patch. However, please note that $header->attachment_id is not always set and you are actually now removing the default get_bloginfo( 'name' ) alt text.

Please note that I have already added my own patch for the issue which is backwards compatible with get_bloginfo( 'name' ) alt text.

#3 @mukesh27
5 months ago

Hi @webmandesign,

If attached image don't have it's alt value than no need to add get_bloginfo( 'name' ) means it's not necessary to have bloginfo when alt it empty.

#4 @webmandesign
5 months ago

Hi @mukesh27,

Thank you. In that case, if there is no need for this backwards compatibility, your solution is much better. I think you don't even have to set $alt variable, you could just use 'alt' => (string) get_post_meta( $header->attachment_id, '_wp_attachment_image_alt', true );

However, please note that as it is possible to filter the image URL via get_header_image(), in my solution I also check if the image is actually the one that should have the alt text - see lines 1096 to 1101 in my patch.

Could someone from core team provide feedback if this is OK please?

#5 @webmandesign
5 months ago

By the way, if it is content image, which custom header in most cases is, the alt should be set to some value. So, it makes sense there is get_bloginfo( 'name' ) used as fallback now.

#6 @webmandesign
5 months ago

  • Keywords 2nd-opinion added

#7 @audrasjb
5 months ago

  • Owner set to audrasjb
  • Status changed from new to reviewing

#8 @audrasjb
5 months ago

  • Keywords needs-refresh added; 2nd-opinion removed
  • Milestone changed from Awaiting Review to 5.2
  • Status changed from reviewing to accepted

Hi,

The idea looks good at a glance. I think we should also add a fallback. Moving this to 5.2.

#9 @webmandesign
5 months ago

Hi @audrasjb,

Thank you for feedback and for accepting the fallback patch.

Could you please also provide some feedback on ticket #46134 as that is somehow related? Thanks!

#10 @afercia
5 months ago

Actually, in most of the cases the header image is purely decorative and in that case it would need an empty alt="" so it gets ignored by assistive technologies.

Only if the image adds some relevant information, the alt attribute would need to have some meaningful content to communicate the image purpose.

Can't recall off the top of my head why it is always populated with the site title. Worth reconsidering because the choice should be in the hands of the site owner but it should be made very clear that in most of the cases the alt attribute should be empty.

Also noting, as in the case of the logo image in #46127, the cropped header image is not listed in the grid view.

#11 @webmandesign
5 months ago

Hi @afercia,

Yes, you're right, all cropped images WordPress creates are affected by #46127.

In the case of empty alt text the fix could be as simple as setting it directly in $attr array as mentioned above.

But there is still an issue here as the custom header image URL could be different from attachment ID in get_header_image_tag(). That way the alt text is pulled from the attachment ID image, yet a different image could be actually displayed... (Look at the first 2 lines of the function code.)
My patch takes care of this issue, but the possibility of different image URL indeed complicates things here, which also relates to other ticket I've reported (#46134).

I think the alt text for the custom header image should be pulled from the actual image if possible and not overwritten or set empty by default. That way user can decide what to do.

As an example, in my theme on single post I'm overriding the custom header image with post featured image. And there is not an easy way of setting correct image alt text for this image now for me if I am using native WordPress functionality only. (I will solve this in my themes with a custom code eventually, but dealing with the issue I've spotted this discrepancy in WP core code.)

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


3 months ago

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


3 months ago

#14 @audrasjb
3 months ago

  • Keywords 2nd-opinion added
  • Milestone changed from 5.2 to 5.3

Hi,

The ticket is still relevant but will need some further work and/or feedback.

That would be great to get some feedback from Media component maintainers and from the Theme Team as well.

I'm moving the ticket to the next release because it's too late for 5.2, but I'll try to ask for feedbacks with the concerned people in the next couple of weeks to make sure the ticket can be handled in 5.3.

#15 @webmandesign
3 months ago

Thank you @audrasjb!

Note: See TracTickets for help on using tickets.