Make WordPress Core


Ignore:
Timestamp:
10/27/2020 05:05:51 PM (5 years ago)
Author:
desrosj
Message:

Twenty Twenty-One: Import the latest changes.

For a full list of changes since [49320], see https://github.com/WordPress/twentytwentyone/compare/461dcf9cd...5759e96.

Props poena, melchoyce, aristath, justinahinon, ryelle.
See #51526.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-content/themes/twentytwentyone/inc/template-functions.php

    r49320 r49330  
    473473}
    474474add_filter( 'the_password_form', 'twenty_twenty_one_password_form' );
     475
     476/**
     477 * Filters the list of attachment image attributes.
     478 *
     479 * @since 1.0.0
     480 *
     481 * @param array        $attr       Array of attribute values for the image markup, keyed by attribute name.
     482 *                                 See wp_get_attachment_image().
     483 * @param WP_Post      $attachment Image attachment post.
     484 * @param string|array $size       Requested size. Image size or array of width and height values
     485 *                                 (in that order). Default 'thumbnail'.
     486 *
     487 * @return array
     488 */
     489function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) {
     490    $width  = false;
     491    $height = false;
     492
     493    if ( is_array( $size ) ) {
     494        $width  = (int) $size[0];
     495        $height = (int) $size[1];
     496    } elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
     497        $meta = wp_get_attachment_metadata( $attachment->ID );
     498        if ( $meta['width'] && $meta['height'] ) {
     499            $width  = (int) $meta['width'];
     500            $height = (int) $meta['height'];
     501        }
     502    }
     503
     504    if ( $width && $height ) {
     505
     506        // Add style.
     507        $attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
     508        $attr['style'] = 'width:100%;height:' . round( 100 * $meta['height'] / $meta['width'], 2 ) . '%;' . $attr['style'];
     509    }
     510
     511    return $attr;
     512}
     513add_filter( 'wp_get_attachment_image_attributes', 'twenty_twenty_one_get_attachment_image_attributes', 10, 3 );
Note: See TracChangeset for help on using the changeset viewer.