Remove width and heigth attributes from WordPress thumbnails

By default, the_post_thumbnail () function displays html post thumbnails in this way:

<img width="400" height="500" src="" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" 
alt="" srcset="" >

In some cases, it is necessary to remove the width and height attributes of the displayed image. To do this, you can use the wp_get_attachment_image_src () function filter:


add_filter('wp_get_attachment_image_src','delete_width_height', 10, 4);

function delete_width_height($image, $attachment_id, $size, $icon){

    $image[1] = '';
    $image[2] = '';
    return $image;
}

This code must be placed in the functions.php file of your theme. You will end up with this HTML:

<img src="" class="attachment-post-thumbnail size-post-thumbnail wp-post-image" 
alt="" srcset="" >

Updated: 18.11.2018

Did this article help you? Rate it!
(No Ratings Yet)
Loading...


Leave a Reply

Your email address will not be published. Required fields are marked *