You can use regex if you want to display it on all existing posts.
WordPress add a wp-image-ATTACHMENT_ID
class on images. We can use it to find the attachment_id
then grab the new url. For example here, with medium size.
wp_get_attachment_image_src
: https://developer.wordpress.org/reference/functions/wp_get_attachment_image_src/
We apply this filter to the content to update the display of your posts. We use regex to find attachment_id
and image src
attribute.
function wpse_289055_new_image_size($content) {
preg_match('/wp-image-[0-9]*/', $content, $matches);
if($matches !== NULL)
{
$thumb = wp_get_attachment_image_src(intval(str_replace('wp-image-', '', $matches[0])), 'medium');
if($thumb)
{
preg_match('/ src="https://wordpress.stackexchange.com/questions/289055/([^"]*)"https://wordpress.stackexchange.com/", $content, $matches);
if(isset($matches[1]))
{
$content = str_replace($matches[1], $thumb[0], $content);
}
}
}
return $content;
}
add_filter('the_content', 'wpse_289055_new_image_size');
In my opinion if you don’t use a cache system, this code use some ressources. Adapt this one to update all your existing post directly.