Filter to remove image dimension attributes?

I’m working on a site based on a fluid width css template which sets a max-width on images to the width of the column containing them, and I need to remove the inline width and height dimension attributes that WordPress adds to images.

I’m doing it with my featured images with this filter:

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;

I know I can apply the same filter to the_content, if necessary. But is there a better way of doing this?


Thanks all!

The image_send_to_editor filter was the one I was looking for… thanks @t31os for pointing it out.

Here’s my functions now.

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

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

This removes inline dimension attributes from images retrieved with the_post_thumbnail(), and prevents those attributes from being added to new images added to the editor. Doesn’t remove them from images retrieved through wp_get_attachment_image or other related functions (no hooks in there), but those cases can be processed in the templates files when necessary.

Leave a Comment