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.
if you set image size in function.php as a “gallery”
1 |
add_image_size( 'gallery', 200, 120, true ); |
you can remove width and height of specific image size such as “gallery”:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// Filter to remove image dimension attributes add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10 ); add_filter( 'image_send_to_editor', 'remove_thumbnail_dimensions', 10 ); // Genesis framework only add_filter( 'genesis_get_image', 'remove_thumbnail_dimensions', 10 ); // Removes attached image sizes as well //add_filter( 'the_content', 'remove_thumbnail_dimensions', 10 ); function remove_thumbnail_dimensions( $html ) { $html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html ); $html = str_replace( 'http://!mysite!', "", $html ); $html = str_replace( 'https://!mysite!', "", $html ); return $html; } |