• 武汉加油,中国加油,免费投稿邮箱 creust@creust.com,vps,服务器,vps 推荐

禁止WordPress头部加载s.w.org来提高网站速度

VPS推荐 创启云客 4个月前 (06-16) 1892次浏览 已收录 0个评论 扫描二维码

禁止WordPress头部加载s.w.org来提高网站速度

wordpress站长有没有发现你的网站 html 头部的 head 中加载了一个代码:

<code><span class="tag"><link</span> <span class="atn">rel</span><span class="pun">=</span><span class="atv">'dns-prefetch'</span> <span class="atn">href</span><span class="pun">=</span><span class="atv">'//s.w.org'</span><span class="tag">></span></code>

WordPress在头部添加 dns-prefetch,应该是为了从s.w.org预获取表情和头像,目的是提高网页加载速度 ,但s.w.org 国内根本无法访问,什么预获取、什么提高速度,都是泡影,不仅没用处,反而可能会影响速度,那就禁止它。

将下面的代码添加到主题functions.php模板中:

方法一

<code><span class="pln">remove_action</span><span class="pun">(</span> <span class="str">'wp_head'</span><span class="pun">,</span> <span class="str">'wp_resource_hints'</span><span class="pun">,</span> <span class="lit">2</span> <span class="pun">);</span></code>

方法二

<code><span class="keyword"><span class="kwd">function</span></span><span class="pln"> remove_dns_prefetch</span><span class="pun">(</span> <span class="vars"><span class="pln">$hints</span></span><span class="pun">,</span> <span class="vars"><span class="pln">$relation_type</span></span> <span class="pun">)</span> <span class="pun">{</span></code> <code><span class="keyword"><span class="kwd">if</span></span> <span class="pun">(</span> <span class="str">'dns-prefetch'</span> <span class="pun">===</span> <span class="vars"><span class="pln">$relation_type</span></span> <span class="pun">)</span> <span class="pun">{</span></code> <code><span class="keyword"><span class="kwd">return</span></span> <span class="func"><span class="pln">array_diff</span></span><span class="pun">(</span><span class="pln"> wp_dependencies_unique_hosts</span><span class="pun">(),</span> <span class="vars"><span class="pln">$hints</span></span> <span class="pun">);</span></code> <code><span class="pun">}</span></code> <code><span class="keyword"><span class="kwd">return</span></span> <span class="vars"><span class="pln">$hints</span></span><span class="pun">;</span></code> <code><span class="pun">}</span></code> <code><span class="pln">add_filter</span><span class="pun">(</span> <span class="str">'wp_resource_hints'</span><span class="pun">,</span> <span class="str">'remove_dns_prefetch'</span><span class="pun">,</span> <span class="lit">10</span><span class="pun">,</span> <span class="lit">2</span> <span class="pun">);</span></code>

方法二貌似兼容性更好些。

附带一个禁止加载表情代码

<code><span class="pln">remove_action</span><span class="pun">(</span><span class="pln"> </span><span class="str">'wp_head'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'print_emoji_detection_script'</span><span class="pun">,</span><span class="pln"> </span><span class="lit">7</span><span class="pln"> </span><span class="pun">);</span></code> <code><span class="pln">remove_action</span><span class="pun">(</span><span class="pln"> </span><span class="str">'wp_print_styles'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'print_emoji_styles'</span><span class="pln"> </span><span class="pun">);</span></code> <code><span class="pln">add_filter</span><span class="pun">(</span><span class="pln"> </span><span class="str">'emoji_svg_url'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'__return_false'</span><span class="pln"> </span><span class="pun">);</span></code>

创启云客 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:禁止WordPress头部加载s.w.org来提高网站速度
喜欢 (0)
[13738388032@qq.com]
分享 (0)
创启云客
关于作者:
vps,服务器,云服务器,第三方支付,免费CDN,免费pos,Hexo,pos机,支付圈,免费vps,Discuz,emlog,主题,日本vps,国外服务器,美国vps,香港vps,便宜vps,便宜服务器,vps服务器
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址