Images have no explicite width and heights
-
My blog is online now and the lazy laoding / webp images are applied. But when testing the page with my mobile device I got the from Page Speed Insights the issue, that there is no explicit widht and height for the images. The issue was not flagged when I run the same test on my Desktop Device.
I have now added the following code to my functions.php:
function add_img_size($content){
$pattern = ‘/]*?src=”(https?:\/\/[^”]+?)”[^>]*?>/iu’;
preg_match_all($pattern, $content, $imgs);
foreach ( $imgs[0] as $i => $img ) {
if ( false !== strpos( $img, ‘width=’ ) && false !== strpos( $img, ‘height=’ ) ) {
continue;
}
$img_url = $imgs[1][$i];
$img_size = @getimagesize( $img_url );if ( false === $img_size ) {
continue;
}
$replaced_img = str_replace( ‘<img ‘, ‘<img ‘ . $img_size[3] . ‘ ‘, $imgs[0][$i] );
$content = str_replace( $img, $replaced_img, $content );
}
return $content;
}
add_filter(‘the_content’,’add_img_size’);`From here: https://webgaku.net/wordpress/add-img-size/
The issue seems to be gone now.
What do you think about it?
Greetings Kathrin
The page I need help with: [log in to see the link]
- The topic ‘Images have no explicite width and heights’ is closed to new replies.