Menu bar

Sunday 28 December 2014

How to remove the width and height from img tag in wordpress posts and pages?


REMOVE WIDTH AND HEIGHT ATTRIBUTES.

It would be great for the images to be able to scale proportionally within their parent container in wordpress to make perfect responsive. This effect can be take place if you remove the width and height attributes from img tag in wordpress.


In order to remove width and height , here we use filter hooks.There are two hooks we will used here. one is post_thumbnail_html and other is image_send_to_editor
 

We will replace width and height attribute with NULL i.e. blank space using preg_replace() function of php.

place this code in functions.php file.
add_filter( 'post_thumbnail_html', 'remove_wps_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'remove_wps_width_attribute', 10 );
 function remove_wps_width_attribute( $html ) {
    $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
    return $html;
}

No comments: