2

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.

  • 3
    When you added the image to the content you selected the size to add, and the HTML for the image is then hardcoded into the content. No wp_get_attachment_ filters or functions are run. You would need to parse the HTML, find image tags, extract the image ID, get the full size of the image, then replace the image tag. – Jacob Peattie Aug 23 '18 at 4:16
  • So I assume that I would have to use the DOM object (after the page is generated) to modify IMG tags to change the image size/width/height attributes, and also modify the SRC attribute to change the image URL? – Rick Hellewell Aug 23 '18 at 19:42
0

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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.