Home / functions.php / Remove width & height attributes from images in posts
  • How to Remove width & height attributes from images in posts

    remove-width-height-attributes-images-posts

    Looking to remove the width and height attributes from images in your posts? Is this because you are using a responsive wordpress theme. Just add this snippet to the functions.php of your wordpress theme and all of the width and height attributes will be removed from your images.

    ( click code to copy )

    wordpress snippet : PHP

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

    ( WordPress codex functions, hooks, in this snippet. )

    add_filter, _e, wp,
  • Websupporter

    If you want to do this for responsive purposes a better way would be to resolve this issue with CSS, since CSS overwrites this attributes:

    img{
    max-width:100%;
    height: auto;
    }