Skip to content

Commit

Permalink
Filter the attachment image attributes for the Twenty Seventeen attac…
Browse files Browse the repository at this point in the history
…hment page.

Per issue #1237, the Twenty Seventeen theme is adding `$attr['sizes']  = '100vw';` which stretches images on the attachment page when in AMP mode.

This commit filters 'wp_get_attachment_image_attributes' to change the layout to responsive and sizes to the image sizes per `wp_get_attachment_image_sizes()`.

AMP uses these attributes to render the <amp-img>.
  • Loading branch information
Tonya Mork committed Aug 6, 2018
1 parent 0ebc97f commit 6baffaa
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions includes/sanitizers/class-amp-core-theme-sanitizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ class AMP_Core_Theme_Sanitizer extends AMP_Base_Sanitizer {
'//header[@id = "masthead"]//a[ contains( @class, "menu-scroll-down" ) ]',
),
'set_twentyseventeen_quotes_icon' => array(),
'add_twentyseventeen_attachment_image_attributes' => array(),
),

// Twenty Sixteen.
Expand Down Expand Up @@ -344,6 +345,30 @@ public static function set_twentyseventeen_quotes_icon() {
} );
}

/**
* Add filter to adjust the attachment image attributes to ensure attachment pages have a consistent <amp-img> rendering.
*
* This is only used in Twenty Seventeen.
*
* @since 1.0
* @link https://github.com/WordPress/wordpress-develop/blob/ddc8f803c6e99118998191fd2ea24124feb53659/src/wp-content/themes/twentyseventeen/functions.php#L545:L554
*/
public static function add_twentyseventeen_attachment_image_attributes() {
add_filter( 'wp_get_attachment_image_attributes', function ( $attr, $attachment, $size ) {
if ( ! is_attachment() ) {
return $attr;
}

$sizes = wp_get_attachment_image_sizes( $attachment->ID, $size );
if ( false !== $sizes ) {
$attr['layout'] = 'responsive';
$attr['sizes'] = $sizes;
}

return $attr;
}, 11, 3 );
}

/**
* Fix up core themes to do things in the AMP way.
*
Expand Down

0 comments on commit 6baffaa

Please sign in to comment.