wordpress正文中图片的srcset属性及https问题

  • 2019-03-11 18:52:06
  • 2,751 次阅读
  • 稿源:天马行空

在之前的一篇文章当中提到过,当开启https之后,浏览器地址栏上的小锁变成了橙黄色,提示网页不安全信息。通过在head加入一个标签后橙黄色的小锁就变成绿色的了。虽然问题解决了,但是图片中的url地址仍然是http的形式,所以今天博主使用函数的方法把http的url替换为https的形式。具体内容如下:

一、出现的问题:

http_problem

src="https://www.maxing128.com/wp-content/uploads/2019/03/Mixed_content.png"

二、解决办法

方法一:
在主题中的functions中加入下面的代码,实现替换功能。

function https_image_replace($content){
if( is_ssl() ){
	$host_name = $_SERVER['HTTP_HOST'];
	$http_host_name='http://'.$host_name.'/wp-content/uploads';
	$https_host_name='https://'.$host_name.'/wp-content/uploads';
	$content = str_replace($http_host_name, $https_host_name, $content);
}
return $content;
}
add_filter('the_content', 'https_image_replace');

方法二:
在phpmyadmin数据库管理中,点击SQL->复制下面的代码到输入框->点击执行即可。

UPDATE wp_posts SET post_content = replace(post_content, 'http://域名/wp-content/uploads','https://域名/wp-content/uploads');

sql

另外,以前就注意到了文章内容图片中的url地址很长,感觉很别扭,有一个screset的属性,如下图
srcset

srcset="https://www.maxing128.com/wp-content/uploads/2019/03/Mixed_content.png 600w, https://www.maxing128.com/wp-
content/uploads/2019/03/Mixed_content-300x234.png 300w" sizes="(max-width: 600px) 100vw, 600px"

博客网站本身做了自适应,该功能不需要,因此需要禁用。方法是在functions.php文件中,添加下面一行代码。

add_filter( 'max_srcset_image_width', create_function('', 'return 1;'));
喜欢 4

文章评论 (1)

  1. hatch Sandwich bar说道:

    Excellent post. I was checking continuously this blog and I’m
    impressed! Extremely useful info specially the last part :) I care for
    such information a lot. I was seeking this certain information for
    a very long time. Thank you and best of luck. http://hatchsandwich.com/

    [1楼]网友 Mac OS X | Chrome 67.0.3396.62   

表情

大眼 可爱 大笑 坏笑 害羞 发怒 折磨 快哭了 大哭 白眼 晕 流汗 困 腼腆 惊讶 憨笑 色 得意 骷髅 囧 睡觉 眨眼 亲亲 疑问 闭嘴 难过 淡定 抗议 鄙视 猪头