I am trying to hook wp_get_attachment_url()
with my custom implementation. I am trying to get my post images 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?
Hook wp_get_attachment_link for this purpose. Here is an example of how to do it –
function attachment_link_filter( $content, $post_id, $size, $permalink ) {
// Only do this if we're getting the file URL
if (! $permalink) {
// This returns an array of (url, width, height)
$image = wp_get_attachment_image_src( $post_id, 'large' );
$new_content = preg_replace('/href=\'(.*?)\"https://wordpress.stackexchange.com/", 'href=\'' . $image[0] . '\'', $content );
return $new_content;
} else {
return $content;
}
}
add_filter('wp_get_attachment_link', 'attachment_link_filter', 10, 4);