url rewriting - Adding a URL Parameter to JPEG images in posts

admin2025-01-07  3

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

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

Share Improve this question edited Jul 11, 2017 at 18:39 kingsgambit asked Jul 10, 2017 at 21:20 kingsgambitkingsgambit 13 bronze badges 5
  • 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 Commented Jul 10, 2017 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 Commented Jul 11, 2017 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 Commented Jul 11, 2017 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 Commented Jul 11, 2017 at 18:21
  • Yes I just found a solution, thanks for your help! – kingsgambit Commented Jul 11, 2017 at 18:29
Add a comment  | 

2 Answers 2

Reset to default 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' );

@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!!

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736258720a535.html

最新回复(0)