参数
$url
string-
给定附件的URL。
$attachment_id
int-
附件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 | 开始引入 |
使用示例
从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; }