当前浏览:首页 / WordPress钩子 / wp_get_attachment_url

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

过滤附件URL

attachment 附件more...

urlmore...


参数

$urlstring
给定附件的URL。
$attachment_idint
附件post ID。

更多信息

wp_get_attachment_url()不区分页面请求是通过HTTP还是HTTPS到达的。

使用wp_get_attachment_url过滤器,我们可以解决这个问题,以避免可怕的混合内容浏览器警告:

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


源码

查看源码 官方文档


更新日志

版本描述
2.1.0开始引入

使用示例

  • 示例1

    从WordPress 3.2.1版开始,wp_get_attachment_url()无法区分页面请求是通过HTTP还是HTTPS到达。

    使用wp_get_attachment_url过滤器,我们可以解决这个问题,以避免可怕的混合内容浏览器警告

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