Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Wordpress URL和wp_获取_附件_图像_src-http与https_Php_Wordpress - Fatal编程技术网

Php Wordpress URL和wp_获取_附件_图像_src-http与https

Php Wordpress URL和wp_获取_附件_图像_src-http与https,php,wordpress,Php,Wordpress,在Wordpress设置中,Wordpress URL(用于许多资源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) $im

在Wordpress设置中,Wordpress URL(用于许多资源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 ... ?>
//wordpress URL设置(在设置->常规中)
http://example.net
//图像源(现在是http://example.net/images/myimage.png)
$imageSource=wp\u获取附件\u图像\u src(获取帖子\u缩略图\u id($post->id),“我的图像”);
?>" .?
如果用户正在访问
https://example.net
,图像仍将从不安全的“http”加载


如何修复此问题,使https中的站点加载https中的所有内容(不仅仅是
wp\u get\u attachment\u image\u src
),反之亦然?

您只需替换URL字符串中的http即可

$imageSource = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "myimage" ); 
$output = preg_replace( "^http:", "https:", $imageSource );
echo $output;

您可以始终将筛选器添加到所需的函数中(例如:
add\u filter('template\u directory\u uri',function($original)
…以始终使用SSL。

仅详细说明@Epik answer-我们应该在HTTP时提供HTTP,在HTTPS时提供HTTPS

我们可以添加一些逻辑,使用内置的Wordpress函数检查是否为_ssl(),然后执行preg替换或使用标准http

    $imageSource = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), "myimage" ); 

    $output = is_ssl() ? preg_replace( "^http:", "https:", $imageSource ) : $imageSource ;
    echo $output;
这是WordPress中的一个问题,计划在WP 4.0中修复

同时,我在以下方面取得了巨大成功:

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

是在所有代码中使用preg replace的唯一解决方案吗?为什么Wordpress函数不考虑用于访问站点的不同协议?当你发表文章时,URL会输入到db post_内容中。这是为了简化函数的构建。如果你不想使用preg_RELAC,你需要以不同的方式处理函数调用例如,您可以获取映像id并使用//而不是http://在您的URL字符串中。总之,是的,您需要以某种方式将http://的实例替换为https://thanks,我不知道你可以为它创建一个过滤器function@cfx有最好的答案,它既解决了为什么这是一个问题,也解决了这个问题的一个非常简单和普遍的解决方案这正是我需要的。谢谢!@cfx很高兴你发现我的小过滤器很有用!