6

I'm using a custom header in my theme. My aim is to add an alt attribute to my custom header img element. My img element so far looks like this:

<img src="<?php header_image(); ?>" height="<?php echo get_custom_header()->height; ?>" width="<?php echo get_custom_header()->width; ?>" alt="" />

How can I get the alt text of the custom header image?

  • Interesting question, one would expect this to be available with get_custom_header()->alt, but that's not the case, as far as I can see after skimming through the source ;-) But fortunately @TheDeadMedic found a great work around. – birgire Jun 26 '14 at 11:00
  • My thoughts exactly regarding get_custom_header()->alt. Thanks to @TheDeadMedic for the solution. – henrywright Jun 26 '14 at 11:34
7
/**
 * Get custom header's alt data.
 * 
 * @link    http://wordpress.stackexchange.com/q/151850/1685
 * 
 * @return  string
 */
function wpse_151850_get_header_image_alt() {
    $attachment_id = 0;

    if ( is_random_header_image() && $header_url = get_header_image() ) {
        // For a random header, we have to search for a match against all headers.
        foreach ( get_uploaded_header_images() as $header ) {
            if ( $header['url'] == $header_url ) {
                $attachment_id = $header['attachment_id'];
                break;
            }
        }

    } elseif ( $data = get_custom_header() ) {
        // For static headers, less intensive approach.
        $attachment_id = $data->attachment_id;
    } 

    if ( $attachment_id ) {
        $alt = trim( strip_tags( get_post_meta( $attachment_id, '_wp_attachment_image_alt', true ) ) );

        if ( ! $alt ) // Fallback to caption (excerpt)
            $alt = trim( strip_tags( get_post_field( 'post_excerpt', $attachment_id ) ) );
        if ( ! $alt ) // Fallback to title
            $alt = trim( strip_tags( get_post_field( 'post_title', $attachment_id ) ) );
    } else {
        $alt = '';
    }

    return $alt;
}
  • One slight modification I'd make to your function is the use of get_custom_header()->attachment_id to get the static header attachment ID instead of $data = get_theme_mod( 'header_image_data' ). Your function then works perfectly. – henrywright Jul 14 '14 at 11:52
  • Nice touch, done. – TheDeadMedic Jul 14 '14 at 15:34
1

Ok I found the answer that no one has on the net I been looking for days now.

Here is how I was able to do it. Hope this helps someone out there

// This is getting the image / url
$feature1 = get_theme_mod('feature_image_1');

// This is getting the post id
$feature1_id = attachment_url_to_postid($feature1);

// This is getting the alt text from the image that is set in the media area
$image1_alt = get_post_meta( $feature1_id, '_wp_attachment_image_alt', true );

Markup

<a href="<?php echo $feature1_url; ?>"><img class="img-responsive center-block" src="<?php echo $feature1; ?>" alt="<?php echo $image1_alt; ?>"></a>

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.