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

一、在主题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 );

二、直接在文件去除相关函数,/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;
// }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。