在 Wordpress 设置中,Wordpress URL(用于很多资源 URL)要求您硬编码 http://
或 https://
网址。这会导致将不安全的部分加载到安全站点上的问题,反之亦然。我该如何处理?
例子:
//The wordpress URL setting (In Settings->General)
http://example.net
//An image source (this is now http://example.net/images/myimage.png)
$imageSource = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "myimage" );
?><img src="<?php echo $imageSource; ?>" .?<?php ... ?>
如果用户正在访问 https://example.net
,图像仍将从非安全的“http”加载。
我该如何解决这个问题,以便 https 中的站点加载 https 中的所有内容(而不仅仅是 wp_get_attachment_image_src
),反之亦然?
最佳答案
这是一个 known defect/bug在 WordPress 中,并计划在 WP 4.0 中修复。
与此同时,here is a filter a WP dev posted我已经取得了巨大的成功:
function ssl_post_thumbnail_urls($url, $post_id) {
//Skip file attachments
if(!wp_attachment_is_image($post_id)) {
return $url;
}
//Correct protocol for https connections
list($protocol, $uri) = explode('://', $url, 2);
if(is_ssl()) {
if('http' == $protocol) {
$protocol = 'https';
}
} else {
if('https' == $protocol) {
$protocol = 'http';
}
}
return $protocol.'://'.$uri;
}
add_filter('wp_get_attachment_url', 'ssl_post_thumbnail_urls', 10, 2);
关于php - Wordpress URL 和 wp_get_attachment_image_src - http 与 https,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24127714/