Skip to content

Instantly share code, notes, and snippets.

@bhaskarkc
Created January 30, 2017 23:46
Show Gist options
  • Save bhaskarkc/9b1bd573dd961e052d79bf1d0434df26 to your computer and use it in GitHub Desktop.
Save bhaskarkc/9b1bd573dd961e052d79bf1d0434df26 to your computer and use it in GitHub Desktop.
WordPress Filter to remove "height" and "width" attributes and values of image only.
<?php
add_filter( 'post_thumbnail_html', 'twr_remove_width_attribute', 10 );
add_filter( 'image_send_to_editor', 'twr_remove_width_attribute', 10 );
add_filter( 'the_content', 'twr_remove_width_attribute', 10);
function twr_remove_width_attribute($html) {
// loop only through img tags
if ( preg_match_all( '/<img[^>]+>/ims', $html, $matches ) ) {
foreach ( $matches as $match ) {
// Replace all occurences of width/height
$clean = preg_replace('/(width|height)=["\'\d%\s]+/ims', "", $match );
// Replace with result within html
$html = str_replace( $match, $clean, $html );
}
}
return $html;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment