Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Fix layout shifts for featured images #692

Merged
merged 5 commits into from
Oct 27, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,3 +472,42 @@ function twenty_twenty_one_password_form( $post = 0 ) {
return $output;
}
add_filter( 'the_password_form', 'twenty_twenty_one_password_form' );

/**
* Filters the list of attachment image attributes.
*
* @since 1.0.0
*
* @param array $attr Array of attribute values for the image markup, keyed by attribute name.
* See wp_get_attachment_image().
* @param WP_Post $attachment Image attachment post.
* @param string|array $size Requested size. Image size or array of width and height values
* (in that order). Default 'thumbnail'.
*
* @return array
*/
function twenty_twenty_one_get_attachment_image_attributes( $attr, $attachment, $size ) {
$width = false;
$height = false;

if ( is_array( $size ) ) {
$width = (int) $size[0];
$height = (int) $size[1];
} elseif ( $attachment && is_object( $attachment ) && $attachment->ID ) {
$meta = wp_get_attachment_metadata( $attachment->ID );
if ( $meta['width'] && $meta['height'] ) {
$width = (int) $meta['width'];
$height = (int) $meta['height'];
}
}

if ( $width && $height ) {

// Add style.
$attr['style'] = isset( $attr['style'] ) ? $attr['style'] : '';
$attr['style'] = 'width:100%;height:' . round( 100 * $meta['height'] / $meta['width'], 2 ) . '%;' . $attr['style'];
}

return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'twenty_twenty_one_get_attachment_image_attributes', 10, 3 );