images - wp_get_attachment_image_attributes not working

admin2025-06-04  3

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.

Share Improve this question asked Jan 24, 2019 at 14:14 D.JCodeD.JCode 2013 silver badges12 bronze badges 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 Commented 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 Commented 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 Commented 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 Commented Jan 24, 2019 at 15:23
Add a comment  | 

1 Answer 1

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

    return $content;
}

add_filter('the_content', 'alter_image_src_in_content');
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748977798a315327.html

最新回复(0)