WordPress.org

Ready to get started?Download WordPress

Forums

wp_get_attachment_image - alt and title attributes missing (3 posts)

  1. Ben Huson
    Member
    Posted 5 years ago #

    The wp_get_attachment_image function does not populate the alt attribute for the image tag.

    I would suggest that the image caption (post_excerpt) should be the image's alt tag content and there should be a title attribute which uses the image title (post_title)?

  2. boquiabierto
    Member
    Posted 5 years ago #

    Taken from the depreceated get_attachment_innerHTML function you could meanwhile add something like

    $attachment_id = (int) $attachment_id;
    if ( !$post = & get_post($attachment_id) )
     return false;
    $title = attribute_escape($post->post_title);
    $alt =  attribute_escape($post->post_content);
    ...
    $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" title="'.attribute_escape($title).'" alt="'.attribute_escape($alt).'" />';
    to the wp_get_attachment_image function. Works well for me but's a dirty hack and this options schould be implemented in the core of future versions…

  3. pengu
    Member
    Posted 5 years ago #

    that works pretty well boquiabierto, except that in the Media editor, the field that is listed as "will be used for alt text" doesn't correspond to post_content. using post_excerpt works a treat though.

Topic Closed

This topic has been closed to new replies.

About this Topic