Skip to content

Instantly share code, notes, and snippets.

@jeremyboggs
Created November 23, 2010 13:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jeremyboggs/711775 to your computer and use it in GitHub Desktop.
Save jeremyboggs/711775 to your computer and use it in GitHub Desktop.
<?php
/**
* Removes inline width and height attributes for images. Thanks to
* @boonebgorges and @wayne_graham for help with this!
*/
function clioweb_remove_inline_sizes($html) {
$pattern = '/(\sheight|\swidth)="(.*?)"/';
$html = preg_replace($pattern, '', $html);
return $html;
}
add_filter( 'get_image_tag', 'clioweb_remove_inline_sizes', 10, 1 );
@waynegraham
Copy link

This only handles one case (height="100"), but height="100px" is not matched here:

There are a few ways to do handle this, here's one:

$patterns = array('/height="[a-zA-Z0-9]+" /', '/width="[a-zA-Z0-9]+" /');

@jeremyboggs
Copy link
Author

Terrific Wayne, thanks! I was actually wondering about that. Will update the code.

@waynegraham
Copy link

In case you didn't see the notice, I smashed the regex into a single check to avoid loop check https://gist.github.com/720074

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment