ALT Tags for custom header image on individual pages

Hi,

My images all have alt tags specified in the media library, and also when inserting them.

I use different custom header images on each page fo my site.

The problem is that when I check the HTML, these header images do not have alt tags. So basically, the alt tags are stripped out.

Is there anything I can do to make the alt tags come through in the custom header images?

I’ve tried modyfying line 10 in the custom-header.php file where it specidies alt tags but nothing has worked so far - also I’m not sure I’m in the right php file :slight_smile:

ANy solution would be greatly appreciated!

Thanks a lot

Hello Gerancos,

It looks like a known WordPress issue:
https://core.trac.wordpress.org/ticket/28861

Please check these links:


Please feel free to ask any other questions that you might have.

Kind Regards, Roman.

Hi Roman

I’ve checked the links you provided and have also tried several plugins for unique header images and others to automatically add alt texts based on image names to images.

However, nothing seems to work - i have unique images on each page (through the “unique headers” plugin) but there doesn’t seem any way to add an alt tag to these images.

Do you (or anyone) know of any workaround for this? I just need one of the following options for the custom header images:

  • retrieve alt text from media library
    OR
  • manually insert the alt text for each image
    OR
  • have the alt tag populate from the image name

Is there any chance to get this to work?

Thanks!

Hi again,

Small redirect: as for the code used on the example on the link in your reply:

function get_custom_header_image_alt() {
    $attachment_id = 0;
    if ( is_random_header_image() ) {
        $header_url = get_header_image();
        if ( $header_url ) {
            foreach ( get_uploaded_header_images() as $header ) {
                if ( $header['url'] == $header_url ) {
                    $attachment_id = $header['attachment_id'];
                    break;
                }
            }
        }
    } else {
        $attachment_id = get_custom_header()->attachment_id;
    }
    if ( $attachment_id ) {
        $alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
        if ( ! $alt )
            $alt = '';
    } else {
        $alt = '';
    }
    return $alt;
}

I would like to try this but have no idea where to insert it. I tried pasting it into custom-headers.php and header.php but it didn’t work.

Thanks!

Hello Gerancos,

It looks like this issue requires close inspection, this goes beyond our support policy. As an option, you can find a reputable freelancer on Upwork for this kind of service.

Kind Regards, Roman.

P. S.
I’m not sure where exactly that code should be used.