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?


1 Answer 1


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=\'(.*?)\'/', 'href=\'' . $image[0] . '\'', $content );
        return $new_content;
    } else {
        return $content;
add_filter('wp_get_attachment_link', 'attachment_link_filter', 10, 4);
  • i tried this but it snot working, as i am not able to see if this function is getting called even. Mar 23, 2013 at 16:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.