1

I have strange issue. I've create custom thumb sizes for Wordpress gallery. So this is my code in functions.php:

add_image_size('square_thumb_1', 1136, 596, true);
add_image_size('square_thumb_2', 568, 568, true);
add_image_size('square_thumb_3', 380, 380, true);
add_image_size('square_thumb_4', 284, 284, true);
add_image_size('square_thumb_5', 228, 228, true);
add_image_size('square_thumb_6', 190, 190, true);
add_image_size('square_thumb_7', 165, 165, true);
add_image_size('square_thumb_8', 142, 142, true);
add_image_size('square_thumb_9', 127, 127, true);
add_filter('shortcode_atts_gallery', 'meks_gallery_atts', 10, 3);

function meks_gallery_atts($output, $pairs, $atts)
{
    if ($atts['columns'] == 1) {
        $output['size'] = 'square_thumb_1';
    } else if ($atts['columns'] == 2) {
        $output['size'] = 'square_thumb_2';
    } else if ($atts['columns'] == 3) {
        $output['size'] = 'square_thumb_3';
    } else if ($atts['columns'] == 4) {
        $output['size'] = 'square_thumb_4';
    } else if ($atts['columns'] == 5) {
        $output['size'] = 'square_thumb_5';
    } else if ($atts['columns'] == 6) {
        $output['size'] = 'square_thumb_6';
    } else if ($atts['columns'] == 7) {
        $output['size'] = 'square_thumb_7';
    } else if ($atts['columns'] == 8) {
        $output['size'] = 'square_thumb_8';
    } else {
        $output['size'] = 'square_thumb_9';
    }
    return $output;
}

Everything is ok except column nr 3 - there should be added square_thumb_3 but it's square_thumb_9, so much smaller than it should...

2
  • That's way too many image-sizes (imho). Think about what will happen after a couple years of uploads when every file uploaded gets cropped to 10 different sizes (many of them close in size). You'll save bandwidth too, if you use the same image size squished down to different sizes, rather than different copies of the image for every possible size it's displayed at.
    – admcfajn
    Oct 13, 2018 at 16:08
  • That's true. I didn't think bout that. But if I display to big image than it should be (eg. 1024px when width of image on site is half of this size) I get errors on GTmetrix or Google Page Speed Insights...
    – Damian
    Oct 13, 2018 at 17:54

1 Answer 1

0

Couldn't you make it shorter by doing the following?

function meks_gallery_atts($output, $pairs, $atts)
{
  $output = array();
  if (in_array($output['size'], array(1,2,3,4,5,6,7,8))) {
    $output['size'] = 'square_thumb_' . $output['size'];
  }else{
    $output['size'] = 'square_thumb_9';
  }
  return $output;
}

It sounds like $atts['columns'] is never 3. Have you tried strict-checking on strings, rather than loose-checking on integers?

is using this: if ($atts['columns'] === '1') { instead of this? if ($atts['columns'] == 1) {

Aside from that... maybe try this to debug & see what that value of 3 really is?

function meks_gallery_atts($output, $pairs, $atts)
{ var_dump(array($output, $pairs, $atts)); exit;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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