解决wordpress开启https后文章图片不显示

 

《解决wordpress开启https后文章图片不显示》

 

最近刚刚开启网站HTTPS,看着在谷歌流量浏览器下打开显示安全的绿色小锁,瞬间觉得高大了不少,

但是发现个问题,就是文章页面图片不显示了,,,,表情瞬间凝固,一个大写的尴尬 ̄□ ̄||

于是f12看了下源码,发现图片还是保持http的状态,,然后谷百了一下,问题解决:

 

找到当前主题下的 function.php 文件,编辑之,在里边代码的末尾追加如下代码:

<div id="crayon-5a1390c347d91621094832-1" class="crayon-line"><span class="crayon-o">/</span><span class="crayon-o">*</span> 替换图片链接为 <span class="crayon-i">https</span> <span class="crayon-o">*</span><span class="crayon-o">/</span></div>
<div id="crayon-5a1390c347d91621094832-2" class="crayon-line crayon-striped-line"><span class="crayon-st">function</span> <span class="crayon-e">https_image_replacer</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-i">content</span><span class="crayon-sy">)</span><span class="crayon-sy">{</span></div>
<div id="crayon-5a1390c347d91621094832-3" class="crayon-line"><span class="crayon-st">if</span><span class="crayon-sy">(</span> <span class="crayon-e">is_ssl</span><span class="crayon-sy">(</span><span class="crayon-sy">)</span> <span class="crayon-sy">)</span><span class="crayon-sy">{</span></div>
<div id="crayon-5a1390c347d91621094832-4" class="crayon-line crayon-striped-line"><span class="crayon-o">/</span><span class="crayon-o">*</span>已经验证使用 <span class="crayon-sy">$</span><span class="crayon-i">_SERVER</span><span class="crayon-sy">[</span><span class="crayon-s">'SERVER_NAME'</span><span class="crayon-sy">]</span>也可以获取到数据,但是貌似<span class="crayon-sy">$</span><span class="crayon-i">_SERVER</span><span class="crayon-sy">[</span><span class="crayon-s">'HTTP_HOST'</span><span class="crayon-sy">]</span>更好一点<span class="crayon-o">*</span><span class="crayon-o">/</span></div>
<div id="crayon-5a1390c347d91621094832-5" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-i">host_name</span> <span class="crayon-o">=</span> <span class="crayon-sy">$</span><span class="crayon-i">_SERVER</span><span class="crayon-sy">[</span><span class="crayon-s">'HTTP_HOST'</span><span class="crayon-sy">]</span><span class="crayon-sy">;</span></div>
<div id="crayon-5a1390c347d91621094832-6" class="crayon-line crayon-striped-line"><span class="crayon-sy">$</span><span class="crayon-i">http_host_name</span><span class="crayon-o">=</span><span class="crayon-s">'http://'</span><span class="crayon-sy">.</span><span class="crayon-sy">$</span><span class="crayon-i">host_name</span><span class="crayon-sy">.</span><span class="crayon-s">'/wp-content/uploads'</span><span class="crayon-sy">;</span></div>
<div id="crayon-5a1390c347d91621094832-7" class="crayon-line"><span class="crayon-sy">$</span><span class="crayon-i">https_host_name</span><span class="crayon-o">=</span><span class="crayon-s">'https://'</span><span class="crayon-sy">.</span><span class="crayon-sy">$</span><span class="crayon-i">host_name</span><span class="crayon-sy">.</span><span class="crayon-s">'/wp-content/uploads'</span><span class="crayon-sy">;</span></div>
<div id="crayon-5a1390c347d91621094832-8" class="crayon-line crayon-striped-line"><span class="crayon-sy">$</span><span class="crayon-i">content</span> <span class="crayon-o">=</span> <span class="crayon-e">str_replace</span><span class="crayon-sy">(</span><span class="crayon-sy">$</span><span class="crayon-i">http_host_name</span><span class="crayon-sy">,</span> <span class="crayon-sy">$</span><span class="crayon-i">https_host_name</span><span class="crayon-sy">,</span> <span class="crayon-sy">$</span><span class="crayon-i">content</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>
<div id="crayon-5a1390c347d91621094832-9" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-5a1390c347d91621094832-10" class="crayon-line crayon-striped-line"><span class="crayon-st">return</span> <span class="crayon-sy">$</span><span class="crayon-i">content</span><span class="crayon-sy">;</span></div>
<div id="crayon-5a1390c347d91621094832-11" class="crayon-line"><span class="crayon-sy">}</span></div>
<div id="crayon-5a1390c347d91621094832-12" class="crayon-line crayon-striped-line"><span class="crayon-e">add_filter</span><span class="crayon-sy">(</span><span class="crayon-s">'the_content'</span><span class="crayon-sy">,</span> <span class="crayon-s">'https_image_replacer'</span><span class="crayon-sy">)</span><span class="crayon-sy">;</span></div>

 

原文:http://www.mobibrw.com/2016/3047

关注公众号,及时获取更多干货
点赞