Make WordPress Core


Ignore:
Timestamp:
07/05/2025 06:55:29 PM (16 months ago)
Author:
adamsilverstein
Message:

Media: expose height and width attributes to the 'wp_get_attachment_image_attributes' filter.

Include the image height and width in the attributes passed to the 'wp_get_attachment_image_attributes' filter. Developers can use this to adjust the width and height attributes returned from the 'wp_get_attachment_image_attributes' function.

Props divinenephron, nacin, Sam_a, wpsmith, anatolbroder, ericlewis, puggan, SergeyBiryukov, spacedmonkey, adamsilverstein, flixos90, sandeepdahiya, SirLouen.
Fixes #14110.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r60285 r60415  
    10711071
    10721072                $attachment = get_post( $attachment_id );
    1073                 $hwstring   = image_hwstring( $width, $height );
    10741073                $size_class = $size;
    10751074
    … …  
    10911090                 * @param string $context The context. Default 'wp_get_attachment_image'.
    10921091                 */
    1093                 $context = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
    1094                 $attr    = wp_parse_args( $attr, $default_attr );
    1095 
    1096                 $loading_attr              = $attr;
    1097                 $loading_attr['width']     = $width;
    1098                 $loading_attr['height']    = $height;
     1092                $context        = apply_filters( 'wp_get_attachment_image_context', 'wp_get_attachment_image' );
     1093                $attr           = wp_parse_args( $attr, $default_attr );
     1094                $attr['width']  = $width;
     1095                $attr['height'] = $height;
     1096
    10991097                $loading_optimization_attr = wp_get_loading_optimization_attributes(
    11001098                        'img',
    1101                         $loading_attr,
     1099                        $attr,
    11021100                        $context
    11031101                );
    … …  
    11701168                $attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment, $size );
    11711169
    1172                 $attr = array_map( 'esc_attr', $attr );
    1173                 $html = rtrim( "<img $hwstring" );
     1170                if ( isset( $attr['height'] ) && is_numeric( $attr['height'] ) ) {
     1171                        $height = absint( $attr['height'] );
     1172                }
     1173                if ( isset( $attr['width'] ) && is_numeric( $attr['width'] ) ) {
     1174                        $width = absint( $attr['width'] );
     1175                }
     1176                unset( $attr['height'], $attr['width'] );
     1177                $attr     = array_map( 'esc_attr', $attr );
     1178                $hwstring = image_hwstring( $width, $height );
     1179                $html     = rtrim( "<img $hwstring" );
    11741180
    11751181                foreach ( $attr as $name => $value ) {
Note: See TracChangeset for help on using the changeset viewer.