WordPress.org

Make WordPress Core

Opened 12 years ago

Closed 12 years ago

#8732 closed defect (bug) (fixed)

Empty Alt Text in wp_get_attachment_image()

Reported by: miqrogroove Owned by: azaozz
Milestone: 2.8 Priority: normal
Severity: normal Version: 2.7
Component: Media Keywords: has-patch tested
Focuses: Cc:

Description

wp_get_attachment_link() calls wp_get_attachment_image() which unconditionally returns an empty alt attribute at line 536:

		$html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';

This is wrong because wp-admin/media.php?action=edit says under the Caption field, "Also used as alternate text for the image". Therefore, wp_get_attachment_image() should be returning the attachment caption.

fwiw, I believe this also affected the 2.6 branch. I decided to report now that I see it's not fixed yet.

Attachments (1)

wp_get_attachment_image_alt.diff (1.4 KB) - added by Sam_a 12 years ago.
Adds ALT and TITLE attributes to img output; adds general filter hook for most attributes

Download all attachments as: .zip

Change History (12)

#1 @miqrogroove
12 years ago

Current workaround

str_replace('alt=""', 'alt="'.get_the_excerpt().'"', wp_get_attachment_link($post->ID, array(450, 800))

#2 @miqrogroove
12 years ago

Also reported #8763 and updated the workaround above as follows:

str_replace('alt=""', 'alt="'.attribute_escape(get_the_excerpt()).'"', wp_get_attachment_link($post->ID, array(450, 800))

#3 @Sam_a
12 years ago

  • Keywords has-patch tested added
  • Owner changed from anonymous to Sam_a
  • Status changed from new to assigned

Attached patch wp_get_attachment_image_alt.diff works in trunk (revision 10347) with no errors.

Patch uses attachment excerpt as alt text ("caption" field in WP 2.6 media interface) if non-empty; otherwise it uses the attachment title. (There was no "caption" field before 2.6, so attachments created then probably have empty excerpts.)

I also added a new filter hook, wp_get_attachment_image_alt, so people can choose some other alt text if they like.

#4 @miqrogroove
12 years ago

Patch installed and working. Thanks a million Sam_a!

#5 @ryan
12 years ago

  • Component changed from General to Media
  • Owner changed from Sam_a to azaozz
  • Status changed from assigned to new

#6 @Sam_a
12 years ago

New patch wp_get_attachment_image_alt.diff:

  • Adds alt attribute (from attachment caption/alternate text field)
  • Adds title attribute (from attachment title field)
  • Adds general wp_get_attachment_image_attributes filter hook allowing plugins to alter attributes (except height, width) or add new ones

This patch treats title and caption fields separately, and the filter hook (on $attr array) is more flexible.

Works for me without errors in revision 10347.

Hopefully Andrew may find it useful. :)

#7 @miqrogroove
12 years ago

Sam_a, I think the attribute quoting needs to be changed back to double quotes for consistency.

#8 @DD32
12 years ago

I think the attribute quoting needs to be changed back to double quotes for consistency.

Shouldn't really matter either way.. Use the right quotation mark where needed.

I do question if the PHPDoc will be reconised in that form though, I'm not too sure if the parser would pick it up, or reconise its to do with that filter at all.

You might be better off mentioning the filters params in the functions PHPDoc instead.

@Sam_a
12 years ago

Adds ALT and TITLE attributes to img output; adds general filter hook for most attributes

#9 @Sam_a
12 years ago

Updated patch wp_get_attachment_image_alt.diff

  • Outputs double-quoted attributes <img alt="" ... />
  • Moved filter hook documentation to function header

#10 @azaozz
12 years ago

  • Milestone changed from 2.7.2 to 2.8

#11 @azaozz
12 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [10744]) Add alt text and filter to wp_get_attachment_image(), props Sam_a, fixes #8732

Note: See TracTickets for help on using tickets.