wordpress去掉<link rel='dns-prefetch' href='//s.w.org' />

本文介绍如何在WordPress中移除指向s.w.org的DNS预获取链接,包括两种方法:通过主题function.php文件屏蔽和直接修改相关函数。这些技巧有助于提高网站在国内的加载速度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  我们在用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;
//		}

  

 

转载于:https://www.cnblogs.com/ytkah/p/9008943.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值