Skip to content

Commit

Permalink
Addresses WordPress#50. Add custom sizes attribute to body images. Re…
Browse files Browse the repository at this point in the history
…lies on Gutenberg #11973.
  • Loading branch information
mor10 committed Nov 17, 2018
1 parent 1427e9b commit cc6c5e5
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions inc/template-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,36 @@ function twentynineteen_post_thumbnail_sizes_attr( $attr ) {
}
add_filter( 'wp_get_attachment_image_attributes', 'twentynineteen_post_thumbnail_sizes_attr', 10, 1 );

/**
* Add custom sizes attribute to responsive image functionality for regular images.
*
* @origin Twenty Nineteen 1.0
*
* @param array $image_attr Attributes for the image markup.
* @param array $block_attr Attributes for the containing block.
* @return string Value for use in post thumbnail 'sizes' attribute.
*/
function twentynineteen_calculate_image_sizes_attr( $image_attr, $block_attr ) {

if ( is_admin() ) {
return $image_attr;
}

$align = $block_attr[align];

if ( '' === $align ) {
$image_attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (max-width: 53rem) calc(8 * (100vw / 12)), (min-width: 53rem) calc(6 * (100vw / 12)), 100vw';
} elseif ( 'wide' === $align ) {
$image_attr['sizes'] = '(max-width: 34.9rem) calc(100vw - 2rem), (min-width: 35rem) 80vw, 100vw';
} elseif ( 'full' === $align ) {
// Use default.
}

return $image_attr;

}
add_filter( 'render_block_core_image_tag_attributes', 'twentynineteen_calculate_image_sizes_attr', 10, 2 );

/**
* Returns the size for avatars used in the theme.
*/
Expand Down

0 comments on commit cc6c5e5

Please sign in to comment.