Skip to content

Instantly share code, notes, and snippets.

@rugor
Created May 1, 2018 17:17
Show Gist options
  • Save rugor/95c6e08eea54adb962460b50a7db36e9 to your computer and use it in GitHub Desktop.
Save rugor/95c6e08eea54adb962460b50a7db36e9 to your computer and use it in GitHub Desktop.
PHP: WordPress remove width and height from image tags for responsive images
/**
* Removes width and height attributes from image tags
*
* @param string $html
*
* @return string
*/
function remove_image_size_attributes( $html ) {
return preg_replace( '/(width|height)="\d*"/', '', $html );
}
// Remove image size attributes from post thumbnails
add_filter( 'post_thumbnail_html', 'remove_image_size_attributes' );
// Remove image size attributes from images added to a WordPress post
add_filter( 'image_send_to_editor', 'remove_image_size_attributes' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment