How to Remove Height and Width From Inserted Images

When we upload an image with the help of WordPress media uploader and insert it into editor, it comes with height and width attributes. These are normally default attributes, as it assists browser to make appropriate room for the image during layout.

But if you want to remove these attributes from image then you can use below code

Just need to put this code in your theme functions.php file

add_filter( 'post_thumbnail_html', 'remove_image_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_image_attribute', 10 );

function remove_image_attribute( $html ) {
   $html = preg_replace( '/(height|width)="\d*"\s/', "", $html );
   return $html;
}

Set Custom Image Size

Function Reference/add image size

Description

Registers a new image size. This means that WordPress will create a copy of the post thumbnail with the specified dimensions when you upload a new thumbnail.

Usage

<?php add_image_size( $name, $width, $height, $crop ); ?>

Parameters

$name
(string) (required) The new image size name.

Default: None
$width
(int) (optional) The post thumbnail width in pixels.

Default: 0
$height
(int) (optional) The post thumbnail height in pixels.

Default: 0
$crop
(boolean) (optional) Crop the image or not. False – Soft proportional crop mode ; True – Hard crop mode.

Default: false

Reserved Image Size Names

thumb, thumbnail, medium, large, post-thumbnail

The names “thumb” & “thumbnail” are just alias, so exactly the same.

However, if needed, you can always set the options yourself:

update_option('thumbnail_size_w', 160);
update_option('thumbnail_size_h', 160);
update_option('thumbnail_crop', 1);

Examples

A theme’s functions.php file.

if ( function_exists( 'add_theme_support' ) ) {
	add_theme_support( 'post-thumbnails' );
        set_post_thumbnail_size( 150, 150 ); // default Post Thumbnail dimensions   
}

if ( function_exists( 'add_image_size' ) ) { 
	add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
	add_image_size( 'homepage-thumb', 220, 180, true ); //(cropped)
}

Crop Mode

Set the image size by resizing the image proportionally (that is, without distorting it):

add_image_size( 'homepage-thumb', 220, 180 ); // 220 pixels wide by 180 pixels tall, soft proportional crop mode

Set the image size by cropping the image (either from the sides, or from the top and bottom):

add_image_size( 'homepage-thumb', 220, 180, true ); // 220 pixels wide by 180 pixels tall, hard crop mode

Using the New Image Sizes

Within a theme’s template files.

<?php if ( has_post_thumbnail() ) { the_post_thumbnail( 'category-thumb' ); } ?>