Make WordPress Core

Ticket #8732: wp_get_attachment_image_alt.diff

File wp_get_attachment_image_alt.diff, 1.4 KB (added by Sam_a, 15 years ago)

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

  • wp-includes/media.php

     
    514514}
    515515
    516516/**
    517  * Retrieve img HTML content for an image to represent an attachment.
     517 * Get an HTML img element representing an image attachment
    518518 *
    519  * @see wp_get_attachment_image_src() Returns img HTML element based on array.
     519 * @uses apply_filters() Calls 'wp_get_attachment_image_attributes' hook on attributes array
     520 * @uses wp_get_attachment_image_src() Gets attachment file URL and dimensions
    520521 * @since 2.5.0
    521522 *
    522523 * @param int $attachment_id Image attachment ID.
     
    533534                $hwstring = image_hwstring($width, $height);
    534535                if ( is_array($size) )
    535536                        $size = join('x', $size);
    536                 $html = '<img src="'.attribute_escape($src).'" '.$hwstring.'class="attachment-'.attribute_escape($size).'" alt="" />';
     537                $attachment =& get_post($attachment_id);
     538                $attr = array(
     539                        'src'   => $src,
     540                        'class' => "attachment-$size",
     541                        'alt'   => trim(strip_tags( $attachment->post_excerpt )),
     542                        'title' => trim(strip_tags( $attachment->post_title )),
     543                        );
     544                $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
     545                $attr = array_map( 'attribute_escape', $attr );
     546                $html = rtrim("<img $hwstring");
     547                foreach ( $attr as $name => $value ) {
     548                        $html .= " $name=" . '"' . $value . '"';
     549                }
     550                $html .= ' />';
    537551        }
    538552
    539553        return $html;