I move all images to a subdomain.after that there is a problem to show some pictures which have srcset
. for the home page, I resolve the problem with the below code
function alter_image_src($attr)
{
$attr['srcset'] = str_replace('/', '/', $attr['srcset']);
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'alter_image_src');
but it does now work for images which are on post pages. Images show this way
<p>
<img class=" size-medium wp-image-467 alignleft" src=".From_.Sky_small-2.png" alt="Zoom.From.Sky_small" width="300" height="160" srcset=".From_.Sky_small-2.png 310w, .From_.Sky_small-2-100x53.png 100w" sizes="(max-width: 300px) 100vw, 300px">
</p>
How can I change all srcset
addresses like the above code. but this time for post pages.
I move all images to a subdomain.after that there is a problem to show some pictures which have srcset
. for the home page, I resolve the problem with the below code
function alter_image_src($attr)
{
$attr['srcset'] = str_replace('https://www.example/wp-content/uploads/', 'https://sub.example/', $attr['srcset']);
return $attr;
}
add_filter('wp_get_attachment_image_attributes', 'alter_image_src');
but it does now work for images which are on post pages. Images show this way
<p>
<img class=" size-medium wp-image-467 alignleft" src="https://sub.example/2015/08/Zoom.From_.Sky_small-2.png" alt="Zoom.From.Sky_small" width="300" height="160" srcset="https://www.example/wp-content/uploads/2015/08/Zoom.From_.Sky_small-2.png 310w, https://www.example/wp-content/uploads/2015/08/Zoom.From_.Sky_small-2-100x53.png 100w" sizes="(max-width: 300px) 100vw, 300px">
</p>
How can I change all srcset
addresses like the above code. but this time for post pages.
I've replaced your string_replace
call with preg_replace
and converted your search string to proper regex (I use regex101 for that). Because you're trying to replace images in the content, which wordpress saves as a string, you need to find anywhere in the content with an image and replace the src
and srcset
for it. This should work (although if you have any script tags with a src
attribute in it then it would attempt to replace those as well. You could probably modify that regex to only match src
and srcset
within an image tag but that's a bit more advanced than my regex skills will take us.
function alter_image_src_in_content($content)
{
$content = preg_replace('~(?:(?:srcset=")|(?:src=")|(?:, ))(https:\/\/www\.example\\/wp-content\/uploads\/)~', 'https://sub.example/', $content);
return $content;
}
add_filter('the_content', 'alter_image_src_in_content');
string_replace()
only works once. if you want to replace all instances of a string you're going to need to usepreg_replace()
and convert your strings to proper regex – mrben522 Commented Jan 24, 2019 at 15:08preg_replace
because the pattern will match multiple times in the srcset string. – mrben522 Commented Jan 24, 2019 at 15:20the_content
filter, and run a regex to find all<img>
tags, then run thatpreg_replace
on each of them in turn – mrben522 Commented Jan 24, 2019 at 15:23