1

I have registered new image size wide-image which should be wrapped inside custom div. I haven't found any wordpress function which gives me the power to check what size of the attached image is used. The size is only in img class like class="size-wide-image wp-image-2924".

What I want to achieve is something like this:

foreach ( attached_image ):
  if ( wide-image ):
    echo '<div class="wrap-wide-image">' . link + attached_image . '</div>';
  endif;
endforeach;

I'm not looking for jQuery solution, I'm looking for php solution. Thank you.

1 Answer 1

0

I think I'd use the get_image_tag filter for this - it provides you with the size as one of the parameters. Something along the lines of:

function wrap_wide_image_tags($html, $id, $alt, $title, $align, $size) {
    if($size == 'wide-image') {
        $html = '<div class="wrap-wide-image">'.$html.'</div>';
    }
    return $html;
}
add_filter('get_image_tag', 'wrap_wide_image_tags', 10, 6);

Hope this helps!

1
  • Thanks for this tip! Although it strips out the href link on first save on post. It is necessary to save and then again edit the image and set the href target again.
    – 5ulo
    Commented Mar 28, 2017 at 20:51

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.