0

I want to filter the src URL of every image (every img tag) on all of my WordPress pages, with the goal of adding a query string to the end of the image URL. (The reasons why are outside the scope of my question here).

My understanding is that I should be able to use the wp_get_attachment_image_src filter to achieve this goal.

I found this example filter which is supposed to do the trick:

function change_src($image, $attachment_id, $size, $icon)
{        
    $image[0] .= '?ver=123';

    return $image;
}
add_filter('wp_get_attachment_image_src', 'change_src', 10, 4);

And there is another function provided at Change Image URL to a CDN which is also supposed to achieve this.

However, when I test either of these two functions on my site, it only changes the src URL of each of my pages' featured images. It does not change the URL of any of the other images on my pages. The other images were all added using the Add Media button in the editor, and my assumption is that all of them should be affected by this filter.

My questions are:

  1. Is it expected behaviour that the wp_get_attachment_image_src filter only affects the featured image of a page, and not the images in the page content which have been added via the Add Media button?

  2. If it's not expected behaviour, then what might be causing this filter not to work on the other images on my site?

  3. If it is expected behaviour, then can you suggest an alternative function that would work on all of my images? Note that I'd prefer to use a proper WordPress filter to achieve this, rather than some kind of search and replace on the page contents.

Also, I'm aware that I need to use the wp_calculate_image_srcset filter to change the URL of the images inside the srcset part of the img tag. I've tested that using the function given at Change Image URL to a CDN and that actually does work correctly on all of the images on my pages. So it's all the more puzzling to me that wp_calculate_image_srcset works on all my images, but wp_get_attachment_image_src does not.

I hope someone can point me in the right direction!

5
  • note that this approach if it did work would only work on save and wouldn't be updatable unless the user removed and re-added the image. Can you share some information about what you're trying to do to remove any doubt? Are you cache busting? Or trying to add server side tracking/dynamic options? What you want might require frontend javascript or runtime PHP filtering after saving, knowing what you're trying to achieve will help narrow down things (or avoid unnecessary questions)
    – Tom J Nowell
    Commented Nov 15, 2021 at 1:00
  • Hi Tom, thanks for replying. Respectfully, I don't agree that wp_get_attachment_image_src only works on save and requires the image to be removed/re-added. As far as I understand, it should dynamically filter the URL when the page is generated at each page load, just as wp_calculate_image_srcset also does. The example function from the other StackExchange answer I linked to, also implies the same. And this filter does successfully work on my featured images - and also my site logo, incidentally - without me needing to remove/resave those.
    – GermanKiwi
    Commented Nov 15, 2021 at 21:54
  • My goal is to achieve cache busting by appending ?ver=xxx to the URL. I'm sure it must be possible to filter the URL of the img src dynamically, just as it's possible to filter the srcset URL using wp_calculate_image_srcset.
    – GermanKiwi
    Commented Nov 15, 2021 at 21:54
  • I see, in that case this solution will never work for post content as you would have to republish every single post to bust the cache, likewise for any other content area
    – Tom J Nowell
    Commented Nov 15, 2021 at 23:25
  • I've left an answer, but you've still fallen into the X Y problem fallacy. You shouldn't have asked how to fix your solution. You should have asked how to solve your original problem instead. There are better ways to solve these problems than this, but that's not the question you asked.
    – Tom J Nowell
    Commented Nov 15, 2021 at 23:35

1 Answer 1

0

Is it expected behaviour that the wp_get_attachment_image_src filter only affects the featured image of a page, and not the images in the page content which have been added via the Add Media button?

Yes, that filter does not run on the image URLs after a post has been saved

If it's not expected behaviour, then what might be causing this filter not to work on the other images on my site?

It is expected behaviour, this filter and others don't run on content that has URLs saved in the database. It only impacts places were an attachment ID needs to be converted at runtime into a URL

If it is expected behaviour, then can you suggest an alternative function that would work on all of my images? Note that I'd prefer to use a proper WordPress filter to achieve this, rather than some kind of search and replace on the page contents.

No such filter exists, you will need to do some kind of search and replace on the page contents. Note that this will then require you to flush all caches, and will be incompatible with a lot of caching plugins and CDN caching systems such as Cloudflare if your goal is to cache bust images. It would be easier to give them new URLs.

Also you will need to adjust many filters and use various output buffers. E.g. you may think it enough to use the_content filter or an output buffer, but this would not fix REST API requests that some JS will use.

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