0

I have a site I'm building in wordpress that has ~80GB of high resolution images, and it's exceeding my managed host's storage space. My plan is to move all the images to S3, but I noticed that wp_get_attachment_image() automatically prefixes the src with /wp-content/uploads instead of respecting the path stored in the database.

Is there a way to hook into that method and modify it to not include the prefix?

2 Answers 2

1

It might be a solution to string replace /wp-content/uploads with '' (empty) like for an example:

Lets say: wp_get_attachment_image() returns = '/wp-content/uploads/myimage.png';

<?php
echo str_replace('/wp-content/uploads/', '', wp_get_attachment_image()); 
?>

This will result in echo: 'myimage.png'

Try this:

function alter_image_src($attr) {
  $attr['src'] = str_replace('/wp-content/uploads/', '', $attr['src']);
  return $attr;
}
add_filter( 'wp_get_attachment_image_attributes', 'alter_image_src');

In your functions page (of theme).

3
  • I would do this, but there are dozens and dozens of places where that method is being called in the admin theme we've picked. Oct 22, 2015 at 19:01
  • Could you try the updated answer? Oct 22, 2015 at 19:11
  • You figured it out before I could reply :D, well done ;) Oct 22, 2015 at 19:13
0

Looks like I can intercept and str_replace in an intercept on wp_get_attachment_image_attributes(), which is used by the higher level wp_get_attachment_image() function.

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.