0

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'.

  • Definitely don't touch the source code. Add a plugin that filters the_content. Within your filter, grab the image last modified date, then output that in the href parameter. This may get you started in the right direction: timbroder.com/2012/12/… – WebElaine Jul 10 '17 at 21:37
  • thanks for pointing me in the right direction. I've edited my original post with a solution attempt. seems to work in my php emulator when i set the content variable but not doing anything in wordpress. – kingsgambit Jul 11 '17 at 4:36
  • To clarify - can you provide an example of one of the href values now - i.e., what is the current URL where you're trying to add the query string? While WP can link an image to an image, it's more common that the image links to an attachment page. – WebElaine Jul 11 '17 at 13:38
  • Right now, on your example page, ?t=### is already being appended to all the images. Are you trying to append the query string to the img src or to an a href? – WebElaine Jul 11 '17 at 18:21
  • Yes I just found a solution, thanks for your help! – kingsgambit Jul 11 '17 at 18:29
0
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' );

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.