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:
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?
If it's not expected behaviour, then what might be causing this filter not to work on the other images on my site?
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!
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:
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?
If it's not expected behaviour, then what might be causing this filter not to work on the other images on my site?
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!
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.
use
function custom_attachment_url($url, $post_id) {
// Check if the attachment is an image
$url .= '?foo=bar';
return $url;
}
add_filter('wp_get_attachment_url', 'custom_attachment_url', 10, 2);
the image[0]
you are using is getting the first image on each page which is the featured image
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 aswp_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:54wp_calculate_image_srcset
. – GermanKiwi Commented Nov 15, 2021 at 21:54