'Without JavaScript, How Can I Append Something on to the SRC URL of an Image in wp_content() in WordPress?

So I've discovered a scriptless way to detect webp support for images. It's fairly simple:

<?php if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false ) { echo '.webp'; } ?>

Easy enough since most programs will convert the image to webp and just append ".webp" after the current extension. It's a great way to serve webp images without the use of plugins or scripts. In case anyone's curious, this is how that's used:

<img src="image.jpg<?php if( strpos( $_SERVER['HTTP_ACCEPT'], 'image/webp' ) !== false ) { echo '.webp'; } ?>" alt="">

Serverside webp detection rather than client site. OK. So my question is this: how do I do this with images placed into the content editor of WordPress? My template simply has this obviously to display that content:

<?php the_content(); ?>

Is there some way, via functions.php or wp-config.php, to use my snippet of code to append all images in the_content()? Plugins can do it with javascript, sure. But I want it to be done without the use of scripts. Just some simple php to say "for every instance of an image in wp_content, if webp is supported, append .webp onto that URL".

Can this be done? Thank you ahead of time!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source