WordPress.org

Make WordPress Core


Ignore:
Timestamp:
10/19/2016 03:05:51 AM (3 years ago)
Author:
joemcgill
Message:

Media: Remove alt fallbacks to improve accessibility.

This removes the fallbacks in wp_get_attachment_image() and in
wp.media.string.props which attempt to generate an alt value
from the image caption or title if an alt attribute isn't explicitly
set.

This allows for image HTML to be generated that contains an empty alt
value, i.e., alt="" which is much preferable for screen readers than
reading redundant content in the case of a caption, or when reading the
image title, which is often generated from the filename and not helpful
as alt text.

Props odie2, joedolson, rianrietveld, afercia, iamjolly, joemcgill.
Fixes #34635.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/media.php

    r38760 r38812  
    917917
    918918    /**
     919     * Tests the default output of `wp_get_attachment_image()`.
     920     * @ticket 34635
     921     */
     922    function test_wp_get_attachment_image_defaults() {
     923        $image = image_downsize( self::$large_id, 'thumbnail' );
     924        $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="" />', $image[1], $image[2], $image[0] );
     925
     926        $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     927    }
     928
     929    /**
     930     * Test that `wp_get_attachment_image()` returns a proper alt value.
     931     * @ticket 34635
     932     */
     933    function test_wp_get_attachment_image_with_alt() {
     934        // Add test alt metadata.
     935        update_post_meta( self::$large_id, '_wp_attachment_image_alt', 'Some very clever alt text', true );
     936
     937        $image = image_downsize( self::$large_id, 'thumbnail' );
     938        $expected = sprintf( '<img width="%1$d" height="%2$d" src="%3$s" class="attachment-thumbnail size-thumbnail" alt="Some very clever alt text" />', $image[1], $image[2], $image[0] );
     939
     940        $this->assertEquals( $expected, wp_get_attachment_image( self::$large_id ) );
     941
     942        // Cleanup.
     943        update_post_meta( self::$large_id, '_wp_attachment_image_alt', '', true );
     944    }
     945
     946    /**
    919947     * @ticket 33878
    920948     */
     
    18091837
    18101838        $expected = '<img width="999" height="999" src="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png"' .
    1811             ' class="attachment-testsize size-testsize" alt="test-image-large.png"' .
     1839            ' class="attachment-testsize size-testsize" alt=""' .
    18121840            ' srcset="http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-testsize-999x999.png 999w,' .
    18131841                ' http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year . '/' . $month . '/test-image-large-150x150.png 150w"' .
Note: See TracChangeset for help on using the changeset viewer.