腾讯云服务器优惠活动华纳云香港服务器

升级WordPress 4.6后禁止WordPress头部加载s.w.org

WordPress主题推荐

升级到WordPress 4.6之后,有童鞋发现头部加载了一个“<link rel=’dns-prefetch’ href=’//s.w.org’>”,WordPress在头部添加dns-prefetch,应该是为了从s.w.org预获取表情和头像,目的是提高网页加载速度 ,但s.w.org国内根本无法访问,什么预获取、什么提高速度,都是泡影,不仅没用处,反而可能会影响速度,那就禁止它。

升级WordPress 4.6后禁止WordPress头部加载s.w.org-第1张-boke112百科(boke112.com)

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

方法一

  1. remove_action( ‘wp_head’, ‘wp_resource_hints’, 2 );

方法二

  1. function remove_dns_prefetch( $hints, $relation_type ) {
  2. if ( ‘dns-prefetch’ === $relation_type ) {
  3. return array_diff( wp_dependencies_unique_hosts(), $hints );
  4. }
  5. return $hints;
  6. }
  7. add_filter( ‘wp_resource_hints’, ‘remove_dns_prefetch’, 10, 2 );

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

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

  1. // Remove emoji script
  2. remove_action( ‘wp_head’, ‘print_emoji_detection_script’, 7 );
  3. remove_action( ‘wp_print_styles’, ‘print_emoji_styles’ );
  4. add_filter( ’emoji_svg_url’, ‘__return_false’ );

参考:https://wordpress.org/support/topic/remove-the-new-dns-prefetch-code/

赞 (0) 打赏
版权声明:本文为转载文章,来源于 知更鸟 ,版权归原作者所有!发布此文是出于传递更多信息之目的,若有来源标注错误或侵犯了您的合法权益,请联系我们,确认后马上更正或删除,谢谢!
wu