WordPress开启https后文章图片转https的方法

WP教程评论7254字数 554阅读模式

WordPress站点开启HTTPS后,文章中引用的图片地址需要由HTTP修改为HTTPS,有两种方法为了解决这个问题。一个是治标不治本的办法,就是在主题的 functions.php 文件中加入字符替换语句,这样在每次访问的时候会相应地把图片地址中的http替换为https,而数据库的原始内容不变;另一个彻底根治的办法是,通过执行SQL语句把文章中的图片地址的http替换为https。

WordPress开启https后文章图片转https的方法

1.function.php文件中新增Hook

function replacehttp($content){
	if( is_ssl() ){
		$content = str_replace('http://domain/wp-content/uploads', 'https://domain/wp-content/uploads', $content);
	}
	return $content;
}
add_filter('the_content', 'replacehttp');

2.通过SQL语句批量替换

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

本文已通过「原本」原创作品认证,转载请注明文章出处及链接。

夏日阳光
  • 本文由 夏日阳光 发表于 2019年4月2日
  • 本文为夏日阳光原创文章,转载请务必保留本文链接:https://www.pieruo.com/49.html
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:
确定

拖动滑块以完成验证