The code I'm currently using in my wordpress functions file is this:
function remove_width_attribute( $html ) {
$html = preg_replace( '/(width|height)=("|\')\d*(|px)("|\')\s/', "", $html );
return $html;
}
add_filter( 'the_content', 'remove_width_attribute', 10 );
The problem here is that it targets iframes too, so how would I rewrite the regex to say images only?
I would just use non capturing groups to search for the img
tag before the other statements, something like
(?:img.*)(width|height ?= ?['"].*['"])
Which you can see working here: http://regex101.com/r/mI1bD5.
Note that this fails in a lot of simple cases (iframe
on the same line as an img
tag for example), but it should push you in the right direction.
Thanks Trevor for helpful suggestion. Here's my revised expression that does a little better, but still not quite there. (?:<img.*){1}((width|height)=['"]\d*['"]) (?:.*\/>|>){1}