Make WordPress Core

Ticket #8256: media_img_caption_shortcode_no_alt.diff

File media_img_caption_shortcode_no_alt.diff, 1.3 KB (added by Sam_a, 17 years ago)

Caption shortcode prints image alternative text only if distinct from caption, allowing override via 'wp_img_caption_print_duplicate_alt' filter

  • wp-includes/media.php

     
    568568 * The supported attributes for the shortcode are 'id', 'align', 'width', and
    569569 * 'caption'.
    570570 *
     571 * If the image's alternative text is identical to the caption text, the alt
     572 * text will be removed (as alt="") unless you pass TRUE via the
     573 * 'wp_img_caption_print_duplicate_alt' filter hook
     574 *
    571575 * @since 2.6.0
    572576 *
    573577 * @param array $attr Attributes attributed to the shortcode.
    574578 * @param string $content Optional. Shortcode content.
     579 * @uses apply_filters() Calls 'img_caption_shortcode' filter
     580 * @uses apply_filters() Calls 'wp_img_caption_print_duplicate_alt' filter
    575581 * @return string
    576582 */
    577583function img_caption_shortcode($attr, $content = null) {
     
    593599
    594600        if ( $id ) $id = 'id="' . esc_attr($id) . '" ';
    595601
     602        if ( ! apply_filters( 'wp_img_caption_print_duplicate_alt', false ) ) {
     603                // alt text is removed if identical to printed caption
     604                $content = str_replace( sprintf( 'alt="%s"', esc_attr($caption) )
     605                        , 'alt=""', $content );
     606        }
     607
    596608        return '<div ' . $id . 'class="wp-caption ' . esc_attr($align) . '" style="width: ' . (10 + (int) $width) . 'px">'
    597609        . do_shortcode( $content ) . '<p class="wp-caption-text">' . $caption . '</p></div>';
    598610}