apply_filters( 'wp_get_attachment_url', string $url, int $attachment_id )

Filters the attachment URL.


Parameters Parameters

$url

(string) URL for the given attachment.

$attachment_id

(int) Attachment post ID.


Top ↑

More Information More Information

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:

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;
}

Top ↑

Source Source

File: wp-includes/post.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
2.1.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Collins Mbaka

    As of WordPress Version 3.2.1, 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

    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;
    }
    

You must log in before being able to contribute a note or feedback.