wordpress去掉dns-prefetch-s.w.org

我们在用wordpress建站时经常会看到<link rel=’dns-prefetch’ href=’//s.w.org’ />,应该是为了从s.w.org预获取表情和头像,目的是提高网页加载速度 ,但s.w.org国内根本无法访问,怎么把这个东西去掉呢?有几种方法:

1、在主题function.php文件中屏蔽

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 );

2、直接在文件去除相关函数,/wp-includes/general-template.php大概2903行全部注释

//		foreach ( $urls as $key => $url ) {
//			$atts = array();
//			if ( is_array( $url ) ) {
//				if ( isset( $url['href'] ) ) {
//					$atts = $url;
//					$url  = $url['href'];
//				} else {
//					continue;
//				}
//			}
//			$url = esc_url( $url, array( 'http', 'https' ) );
//			if ( ! $url ) {
//				continue;
//			}
//			if ( isset( $unique_urls[ $url ] ) ) {
//				continue;
//			}
//			if ( in_array( $relation_type, array( 'preconnect', 'dns-prefetch' ) ) ) {
//				$parsed = wp_parse_url( $url );
//				if ( empty( $parsed['host'] ) ) {
//					continue;
//				}
//				if ( 'preconnect' === $relation_type && ! empty( $parsed['scheme'] ) ) {
//					$url = $parsed['scheme'] . '://' . $parsed['host'];
//				} else {
//					// Use protocol-relative URLs for dns-prefetch or if scheme is missing.
//					$url = '//' . $parsed['host'];
//				}
//			}
//			$atts['rel'] = $relation_type;
//			$atts['href'] = $url;
//			$unique_urls[ $url ] = $atts;
//		}
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论