php - Wordpress URL 和 wp_get_attachment_image_src - http 与 https

标签 php wordpress

在 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/

相关文章:

javascript - 无法使用 jQuery 和 AJAX 发送 POST 请求

html - Contact Form 7 中的占位符 - Wordpress

php - 根据选择器选择显示自定义结账字段

php - Divi Builder 自定义模块

wordpress - 革命 slider YouTube 视频静音延迟

php - 搜索获取结果空字段

php - 如何通过递归重新分配值?

PHP <---> FLASH长连接(数据流)

php - 从动态 Php 页面生成 HTML 静态页面

css - col-xs-6 Bootstrap 堆叠为 col-xs-12