WordPress.org

Make WordPress Core

Ticket #14110: wp_get_attachment_image.diff

File wp_get_attachment_image.diff, 3.4 KB (added by divinenephron, 4 years ago)

Patch to give the 'wp_get_attachment_image_attributes' filter access to the heigh and width attribues.

  • wordpress/wp-includes/media.php

     
    7979} 
    8080 
    8181/** 
    82  * Retrieve width and height attributes using given width and height values. 
    83  * 
    84  * Both attributes are required in the sense that both parameters must have a 
    85  * value, but are optional in that if you set them to false or null, then they 
    86  * will not be added to the returned string. 
    87  * 
    88  * You can set the value using a string, but it will only take numeric values. 
    89  * If you wish to put 'px' after the numbers, then it will be stripped out of 
    90  * the return. 
    91  * 
    92  * @since 2.5.0 
    93  * 
    94  * @param int|string $width Optional. Width attribute value. 
    95  * @param int|string $height Optional. Height attribute value. 
    96  * @return string HTML attributes for width and, or height. 
    97  */ 
    98 function image_hwstring($width, $height) { 
    99         $out = ''; 
    100         if ($width) 
    101                 $out .= 'width="'.intval($width).'" '; 
    102         if ($height) 
    103                 $out .= 'height="'.intval($height).'" '; 
    104         return $out; 
    105 } 
    106  
    107 /** 
    10882 * Scale an image to fit a particular size (such as 'thumb' or 'medium'). 
    10983 * 
    11084 * Array with image url, width, height, and whether is intermediate size, in 
     
    218192function get_image_tag($id, $alt, $title, $align, $size='medium') { 
    219193 
    220194        list( $img_src, $width, $height ) = image_downsize($id, $size); 
    221         $hwstring = image_hwstring($width, $height); 
    222  
    223         $class = 'align' . esc_attr($align) .' size-' . esc_attr($size) . ' wp-image-' . $id; 
    224         $class = apply_filters('get_image_tag_class', $class, $id, $align, $size); 
    225  
    226         $html = '<img src="' . esc_attr($img_src) . '" alt="' . esc_attr($alt) . '" title="' . esc_attr($title).'" '.$hwstring.'class="'.$class.'" />'; 
    227195 
     196  $class = "align$align size-$size wp-image-$id"; 
     197  $attr = array( 
     198    'src'       => $img_src, 
     199    'width' => intval($width), 
     200    'height' => intval(height), 
     201                'class' => $class, 
     202    'alt'       => $alt, 
     203    'title'     => $title 
     204  ); 
     205  $attr = array_map( 'esc_attr', $attr ); 
     206  $attr['class'] = apply_filters('get_image_tag_class', $attr['class'], $id, $align, $size); 
     207 
     208  $html = rtrim("<img "); 
     209        foreach ( $attr as $name => $value ) { 
     210          $html .= " $name=" . '"' . $value . '"'; 
     211        } 
     212  $html .= ' />'; 
    228213        $html = apply_filters( 'get_image_tag', $html, $id, $alt, $title, $align, $size ); 
    229214 
    230215        return $html; 
     
    633618 * @return string HTML img element or empty string on failure. 
    634619 */ 
    635620function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') { 
    636  
    637621        $html = ''; 
    638622        $image = wp_get_attachment_image_src($attachment_id, $size, $icon); 
    639623        if ( $image ) { 
    640624                list($src, $width, $height) = $image; 
    641                 $hwstring = image_hwstring($width, $height); 
    642625                if ( is_array($size) ) 
    643626                        $size = join('x', $size); 
    644627                $attachment =& get_post($attachment_id); 
    645628                $default_attr = array( 
    646629                        'src'   => $src, 
     630      'height' => intval($height), 
     631      'width' => intval($width), 
    647632                        'class' => "attachment-$size", 
    648633                        'alt'   => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first 
    649634                        'title' => trim(strip_tags( $attachment->post_title )), 
     
    656641                $attr = wp_parse_args($attr, $default_attr); 
    657642                $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); 
    658643                $attr = array_map( 'esc_attr', $attr ); 
    659                 $html = rtrim("<img $hwstring"); 
     644                $html = rtrim("<img "); 
    660645                foreach ( $attr as $name => $value ) { 
    661646                        $html .= " $name=" . '"' . $value . '"'; 
    662647                }