Change the URL of an image from wp_get_attachment_image_src

I call a lot of images with wp_get_attachment_image_src() and would like to off load those images to a CDN.

Is there a filter to grab the URL and change it?

Related posts

2 comments

  1. No, not strictly. You can check the source and see that there is no hook that would let you alter the URL.

    You should also notice this interesting bit of code:

    512         if ( $image = image_downsize($attachment_id, $size) )
    513                 return $image;
    

    Follow the trail to here and you get this:

    141         // plugins can use this to provide resize services
    142         if ( $out = apply_filters( 'image_downsize', false, $id, $size ) )
    143                 return $out;
    

    If you hook into image_downsize

    add_filter(
      'image_downsize',
      function ($f,$id,$size) {
        // your own downsize function
    
      },
      10,3
    );
    wp_get_attachment_image_src(4);
    

    … you should be able to return any URL you want, but it means you will need to duplicate more or less the whole of image_downsize() with changes of course.

Comments are closed.