Title: wp_get_attachment_url
Published: April 25, 2014
Last modified: February 24, 2026

---

# apply_filters( ‘wp_get_attachment_url’, string $url, int $attachment_id )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#wp--skip-link--target)

Filters the attachment URL.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#parameters)󠁿

 `$url`string

URL for the given attachment.

`$attachment_id`int

Attachment post ID.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#more-information)󠁿

[wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)
doesn’t distinguish whether a page request arrives via HTTP or HTTPS.

Using wp_get_attachment_url filter, we can fix this to avoid the dreaded mixed content
browser warning:

    ```wp-block-preformatted
    add_filter('wp_get_attachment_url', 'honor_ssl_for_attachments');
    function honor_ssl_for_attachments($url) {
    	$http = site_url(FALSE, 'http');
    	$https = site_url(FALSE, 'https');
    	return ( $_SERVER['HTTPS'] == 'on' ) ? str_replace($http, $https, $url) : $url;
    }
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#source)󠁿

    ```php
    $url = apply_filters( 'wp_get_attachment_url', $url, $post->ID );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/post.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/post.php#L7027)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/post.php#L7027-L7027)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#related)󠁿

| Used by | Description | 
| [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)`wp-includes/post.php` |

Retrieves the URL for an attachment.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.1.0](https://developer.wordpress.org/reference/since/2.1.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/?output_format=md#comment-content-4613)
 2.   [Collins Mbaka](https://profiles.wordpress.org/collinsmbaka/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/wp_get_attachment_url/#comment-4613)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_url%2F%23comment-4613)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_url%2F%23comment-4613)
 4. As of WordPress Version 3.2.1, [wp_get_attachment_url()](https://developer.wordpress.org/reference/functions/wp_get_attachment_url/)
    doesn’t distinguish whether a page request arrives via HTTP or HTTPS.
 5. Using wp_get_attachment_url filter, we can fix this to avoid the dreaded mixed 
    content browser warning
 6.     ```php
        add_filter( 'wp_get_attachment_url', 'wpdocs_honor_ssl_for_attachments' );
        function wpdocs_honor_ssl_for_attachments( $url ) {
        	$http = site_url( FALSE, 'http' );
        	$https = site_url( FALSE, 'https' );
        	return ( 'on' === $_SERVER['HTTPS'] ) ? str_replace( $http, $https, $url ) : $url;
        }
        ```
    
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_url%2F%3Freplytocom%3D4613%23feedback-editor-4613)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fwp_get_attachment_url%2F)
before being able to contribute a note or feedback.