Proper way to hook wp_get_attachment_url or any other way to change that url in media library

I am trying to hook wp_get_attachment_url() with my custom implementation. I am trying to get my post videos and any other static data from the Amazon S3 and I was wondering if I can configure the URL with my Amazon bucket URL.

This is what I was trying to do

add_filter('wp_get_attachment_url', 'custom_get_attachment_url', 1, 1);

function clrs_get_attachment_url($post_id = 0) {
  // change URL for Amazon bucket
}

but this is not working as expected as I am getting $post_id as 0. How to do it in a proper way?

1 Answer
1

The filter has 2 parameters, also the function name doesn’t match. So your code should look like this:

add_filter('wp_get_attachment_url', 'clrs_get_attachment_url', 10, 2);

function clrs_get_attachment_url($url, $post_id) {
   // Do what you want to $url
   return $url;
}

Leave a Comment