Force all images to full size in page template

I have a page template that outputs all posts with minimal formatting – no headers, footers, sidebars, etc.

With the loop, the_content() is used to output content (text and images).

I need to output all images in ‘full’ or ‘large’ size. Currently, the_content() in the loop is returning an img tag similar to this – always thumbnail.

    <img 
src="https://www.example.com/wp-content/uploads/sites/21/2016/10/sample-picture-150x150.jpg" 
    class="attachment-thumbnail size-thumbnail" alt="" 
    size="full" width="150" height="150">

This causes the resolution of the image to be less than what I need.

I tried using the wp_get_attachment_image_attributes filter to force the size attribute to ‘full’, but that didn’t work:

function my_fix_attachment_size($attr, $attachment, $size) {
  // Full width header images
    $attr['size'] = 'full';
  return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'my_fix_attachment_size', 10 , 3);

How to I get the_content() to output full size images? In the post, there could be images or galleries, so need to handle both.

Note that I do not need to set/change image sizes, but just force full size in the generated page.I am also not worried about the viewport size, as the generated page is used to create a document.

Added

After much googles and expirementation, I came up with a solution that worked for me (below as an answer). I suspect there are better ways to do this, but this is the only one that worked for me.

Additional answers are welcomed.

1 Answer
1

This code works, although I suspect that it could be more efficient – or replaced by a better filter. I modified this code from the answer to this question, which was the best choice out of all the googles I spent hours trying out.enter link description here

You will need to change the ‘full’ to the size you want to have (in two places). You can also change other array elements to your needs (after the ‘extract’ function).

I’m open to easier/more efficient ways to do this.

 function change_image_size ($output, $attr) {
        global $post;  // needed to use in the id element
        extract(shortcode_atts(array(
            'order' => 'ASC',
            'orderby' => 'menu_order ID',
            'id' => $post->ID,
            'itemtag' => 'dl',
            'icontag' => 'dt',
            'captiontag' => 'dd',
            'columns' => 3,
            'size' => 'thumbnail',
            'include' => '',
            'exclude' => ''
        ), $attr));
        // here's where you can change/add an attribute to the shortcode
        $attr['size'] = 'full'; // change 'full' to desired size
        $id = intval($id);
        if ('RAND' == $order) {
            $orderby = 'none';
        }

        if (!empty($include)) {
            $include = preg_replace('/[^0-9,]+/', '', $include);
            $_attachments = get_posts(array('include' => $include, 
'post_status' => 'inherit', 'post_type' => 'attachment',
 'post_mime_type' => 'image', 'order' => $order,
 'orderby' => $orderby));

            $attachments = array();
            foreach ($_attachments as $key => $val) {
                $attachments[$val->ID] = $_attachments[$key];
            }
        } elseif (!empty($exclude)) {
            $exclude = preg_replace('/[^0-9,]+/', '', $exclude);
            $attachments = get_children(array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby));
        } else {
            $attachments = get_children(array('post_parent' => $id,
 'post_status' => 'inherit', 'post_type' => 'attachment', 
'post_mime_type' => 'image', 'order' => $order,
 'orderby' => $orderby));
        }

        if (empty($attachments)) {
            return '';
        }

    // Essentially these are only changes I've made
    // you can change the $output to your needs; including changing 'full' to your desired image size.
        $output="";
        foreach ($attachments as $att_id => $attachment) {
            $output .= '<figure>' . wp_get_attachment_image($att_id, 'full') .

'<figcaption>' . wptexturize($attachment->post_excerpt) . 
'</figcaption></figure>';
        }   // change 'full' to desired size

        return $output;
    }
    add_filter('post_gallery', 'change_image_size', 10, 2);

Leave a Comment