wp_get_attachment_url WordPress Filter Hook

The wp_get_attachment_url hook is used to get the URL of an attachment. This can be useful for getting the URL of an image or other media file.

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

Filters the attachment URL.


Parameters

$url

(string)URL for the given attachment.

$attachment_id

(int)Attachment post ID.


Top ↑

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

File: wp-includes/post.php

View on Trac



Top ↑

Changelog

Changelog
VersionDescription
2.1.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by the Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More