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

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


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!

  • 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 Mar 28 '17 at 20:51

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.