0

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.com/wp-content/uploads/', 'https://sub.example.com/', $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.com/2015/08/Zoom.From_.Sky_small-2.png" alt="Zoom.From.Sky_small" width="300" height="160" srcset="https://www.example.com/wp-content/uploads/2015/08/Zoom.From_.Sky_small-2.png 310w, https://www.example.com/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.

4
  • string_replace() only works once. if you want to replace all instances of a string you're going to need to use preg_replace() and convert your strings to proper regex
    – mrben522
    Jan 24, 2019 at 15:08
  • @mrben522 if I want to use it once and just for post image, how can I do?
    – D.JCode
    Jan 24, 2019 at 15:18
  • I misread your question, I'll update my answer shortly. You still need to use preg_replace because the pattern will match multiple times in the srcset string.
    – mrben522
    Jan 24, 2019 at 15:20
  • actually this just got super complicated. you need to use the_content filter, and run a regex to find all <img> tags, then run that preg_replace on each of them in turn
    – mrben522
    Jan 24, 2019 at 15:23

1 Answer 1

0

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\.com\/wp-content\/uploads\/)~', 'https://sub.example.com/', $content);

    return $content;
}

add_filter('the_content', 'alter_image_src_in_content');
2
  • thanks for your reply. it works correctly for the home page as same as my code. but for images post the problem is not solve https://bit.ly/2MwdEJ5
    – D.JCode
    Jan 24, 2019 at 15:23
  • If you have no images where the src is showing incorrectly then you can remove |(?:src=") from that regex.
    – mrben522
    Jan 24, 2019 at 15:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.