Unexpected width and srcset attributes for the_post_thumbnail();

When generating the width and height attributes for an image with functions like the_post_thumbnail() or wp_get_attachment_image() the images are put through the image_downsize() function. The last thing this function does is run the image through the image_constrain_size_for_editor() function.

This function decides the width and height of the image based on the given size, large in your case, based on the settings in Settings > Media. However it will set the maximum possible size of medium, medium_large and large images to whatever the theme’s Content Width is defined as. If your theme defines the content width as 800 then images with the medium, medium_large or large size will have their width capped at 800 (custom image sizes are only affected by this in the editor).

If you want to remove the maximum dimensions, you can use the editor_max_image_size filter to return an array containing your desired maximum width and height or 0 for both to remove the limit. You can use the $size and $context arguments of the filter to only apply to large images on the front-end or back-end ('display' for front-end, 'edit' for admin).

This example will remove the size limit for large images on the front-end:

function wpse_308298_max_image_size( $max_image_size, $size, $context ) {
    if ( $size === 'large' && $context === 'display' ) {
        $max_image_size = [0,0];
    }

    return $max_image_size;
}
add_filter( 'editor_max_image_size', 'wpse_308298_max_image_size' );

Leave a Comment