Created
January 30, 2017 23:46
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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