WordPress 更新到4.6版本后,在头部新加了 dns-prefetch,好像是为了从s.w.org预获取表情和头像,从而提高网页的加载速度,不过s.w.org从国内无法访问,所以 WordPress 新加的 dns-prefetch不但不能提高网页的打开速度,反而会对速度有影响,因此需要禁掉它。

WordPress 加了 dns-prefetch, 头部源代码中会有:

<link rel=‘dns-prefetch’ href=’//s.w.org’ />

禁掉的方法有两个,根据兼容性情况选择合适的一个即可。

第一个:

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

第二个:

function remove_dns_prefetch( $hints, $relation_type ) { if ( ‘dns-prefetch’ === $relation_type ) { return array_diff( wp_dependencies_unique_hosts(), $hints ); } return $hints; } add_filter( ‘wp_resource_hints’, ‘remove_dns_prefetch’, 10, 2 );