How do you remove hard coded thumbnail image dimensions?

How can I remove the width and height attributes from the post_thumbnail when inserting with <?php the_post_thumbnail(); ?>?

<img width="800" height="533" src="http://domain.com/wp-content/uploads/2011/02/image.jpg" class="attachment-post-thumbnail wp-post-image" />

Related posts

Leave a Reply

4 comments

  1. Related: Filter to remove image dimension attributes?

    There’s a filter on post_thumbnail_html which receives as its argument the full html element representing the post thumbnail image before it’s echoed to the page. You can filter out the dimensions with a bit of regex:

    add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
    
    function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
        $html = preg_replace( '/(width|height)="d*"s/', "", $html );
        return $html;
    }
    
  2. you could just grab the url of the thumb and put it in an img tag yourself:

    <?php
    $thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'your_thumb_handle' );
    ?>
    <img src="<?php echo $thumbnail['0']; ?>" />
    
  3. add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 );
    add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 );
    add_filter( 'the_content', 'remove_thumbnail_dimensions', 10 );
    function remove_thumbnail_dimensions( $html ) {
        $html = preg_replace( '/(width|height)="d*"s/', "", $html );
        return $html;
    }
    

    this will do the job, “the_contnet” will remove all post content text image width and height.

  4. I prefer this solution below as I’m not doing a global replace with a function. This would be incorporated into your theme files.

    <?php echo preg_replace( '/(width|height)="d*"/', '', get_the_post_thumbnail( get_the_ID(), 'large' ) ); ?>
    

    You can replace “large” with “thumbnail”, “medium”, “full”, or your custom image size declared in your theme.