Adding a URL Parameter to JPEG images in posts

To avoid image caching issues, I would like to get WordPress to reference my jpeg images with a URL parameter. I know in javascript I can do this:

img id="idSigma" src="#" class="classSigma"
script
$(document).ready(function() {
  var d = new Date();
  $("#idSigma").attr("src", "images/Sigma.jpeg?t=" + d.getTime());
});
/script

Is there a way I can get wordpress to do this to all of its internal links. For example, if I right click on an image in a blog post and click on open image it would already be pointing to the link with the URL parameter. This will ensure all the images are fresh since I plan on updating them daily. My first thought was to look for this code in media.php but maybe there is a better way than modifying the source code.


Edit, here is what I have so far, it works in my php emulator when I set the $content variable but not doing anything in wordpress:

function add_jpeg_params($content){

preg_match_all("/https?:\/\/[^\/\s]+\/\S+\.(jpg|jpeg)/", $content, $output_array);

$items_to_replace = array_unique($output_array[0]);
$items_to_replace = array_values($items_to_replace);

for ($j = 0; $j  sizeof($items_to_replace); $j++) {

    $content = preg_replace('~' . $items_to_replace[$j] . '~', $items_to_replace[$j] . '?t=' . time(), $content);
} 
return $content;
}
add_filter('the_content','add_jpeg_params');

I've added this in functions.php in my wordpress theme.

Edit 2: Solution posted below. The hook I needed was 'post_thumbnail_html'.

Topic url-rewriting images cache Wordpress

Category Web


@kingsgambit

I want to pass the following URL Parameters at the end of every image URL in WordPress site: ?f=auto

I am using WooCommerce and wanted to add following URL Parameters at the end of every image in my WordPress site.

Can you please help me with that? Regards!!


function add_jpeg_params($content){

preg_match_all("/https?:\/\/[^\/\s]+\/\S+\.(jpg|jpeg)/", $content, $output_array);

$items_to_replace = array_unique($output_array[0]);
$items_to_replace = array_values($items_to_replace);

for ($j = 0; $j < sizeof($items_to_replace); $j++) {

    $content = str_replace($items_to_replace[$j], $items_to_replace[$j] . '?t=' . time(), $content);
}

return $content;
}
add_filter('the_content','add_jpeg_params',10);
add_filter('post_thumbnail_html', 'add_jpeg_params' );

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.