partial solution to WP responsive images
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 | |
/* | |
* full version of code I added in a comment on wpscholar.com about how | |
* make it easier for WP to produce responsive images | |
* @link https://wpscholar.com/blog/remove-wp-image-size-attributes/#comment-23731 | |
* | |
* Note: I make no claims that this handles all cases, just those problems | |
* identified by others commenting on the OP's code | |
*/ | |
// 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', 21 ); | |
// Tell the [caption] shortcode not to output a "style: width: xxxpx" HTML attribute | |
add_filter( 'img_caption_shortcode_width', '__return_false' ); | |
/** | |
* Removes width and height attributes from image tags | |
* | |
* @param string $html | |
* @return string | |
*/ | |
function remove_image_size_attributes( $html ) { | |
// Find the <img> tag (so that we don't strip width from a [caption] shortcode) | |
preg_match_all( '!<img [^>]+>!i', $html, $matches, PREG_SET_ORDER ); | |
if ( ! empty( $matches ) ) { | |
$html = str_replace( $matches[0][0], preg_replace( '/(width|height)="\d*" /', '', $matches[0][0] ), $html ); | |
} | |
return $html; | |
} | |
/** | |
* Tell the [caption] shortcode not to output a "style: width: xxxpx" HTML attribute | |
* | |
* @param string $html | |
* @return string | |
*/ | |
function img_caption_shortcode_width() { | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment