How to Remove Hardcore Width and Height of Thumbnail size in WordPress

By default, WordPress Featured image comes with the built in width and height. In some cases, your website may not meet the requirement of the height/width of the image. Or let’s say, you are not familiar with the responsive design and your images show messed up width and height after displaying on the front end. This will be the case when you are developing your custom theme or custom template. We will need to have to get removed hardcore width and height of thumbnail size dimensions using simple filter hook. This will not only remove the core width or height generated by default WordPress but also gives us an ease access to modify our own custom size.

So We Will be Using

apply_filters( 'post_thumbnail_html', '$variables' );

hook to filter the post thumbnail HTML.

This post_thumbnail_html hook will pass 5 variables to your hooked function.

  • $html – the Output HTML of the post thumbnail
  • $post_id – the post ID.
  • $post_thumbnail_id – the attachment id of the image
  • $size – the size requested or default
  • $attr – Query string or array of attributes.

When you are uploading the images via Image Uploader in Featured image in your posts or pages, the WordPress will automatically include its default width and height attribute in the HTML <img> tag. It will look something like this when you view in HTML.

<img width="123" height="456" scr="http://prabinparajuli.com.np/wp-content/uploads/2016/12/Disable-Comments-in-Pages-or-Posts-65x65.jpg">

Remove Hardcore Width and Height of Thumbnail size

Copy the following code in your theme’s functions.php

// Removing Hardcore width and Height in WordPress
add_filter( 'post_thumbnail_html', 'pp_remove_thumbnail_dimensions', 10, 3 );
function pp_remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

That’s it. The default width and height attribute will not be there when you insert the image into post thumbnail the next time.

ALSO READ  Allow Shortcode for Custom Widget in WordPress

Source 

There are also few plugins available in WordPress Repository to remove the image attributes. If you don’t feel like using a code, you can use the following plugins. With a few clicks, you can modify or remove the image size attributes.