wordpress去除头部的无用短连接,必须删
浏览: 3,844 次 --使用wordpress搭建博客,非常方便,但正常安装wordpress,查看源代码,head标签中存在一些无用标签,而且影响页面加载速度。
其实这些的调用是由 wp_head() 引进来的,只需在当前主题文件 functions.php 中,插入以下代码,将无用额短连接移除即可精简源代码。
//移除feed
remove_action( 'wp_head', 'feed_links', 2 ); //文章和评论feed remove_action( 'wp_head', 'feed_links_extra', 3); //分类等feed
//移除头部Previous 和 Next 文章链接
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
//移除文章shortlink短链接
remove_action('wp_head', 'wp_shortlink_wp_head');
//移除Wordpress的版本号
remove_action('wp_head', 'wp_generator');
//移除REST API功能
add_filter('rest_enabled', '__return_false'); add_filter('rest_jsonp_enabled', '__return_false');
//移除wp-json链接
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 ); remove_action( 'wp_head', 'wp_oembed_add_discovery_links', 10 );
//移除离线编辑器开放接口
remove_action( 'wp_head', 'rsd_link' ); //移除EditURI remove_action( 'wp_head', 'wlwmanifest_link' );
//移除本页页面链接URL
remove_action( 'wp_head', 'rel_canonical' );
//去除头部的<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 );
//禁用WordPress Emoji表情
remove_action('admin_print_scripts' , 'print_emoji_detection_script'); remove_action( 'admin_print_styles' , 'print_emoji_styles'); remove_action( 'wp_head' , 'print_emoji_detection_script', 7); remove_action( 'wp_print_styles' , 'print_emoji_styles'); remove_filter( 'the_content_feed' , 'wp_staticize_emoji'); remove_filter( 'comment_text_rss' , 'wp_staticize_emoji'); remove_filter( 'wp_mail' , 'wp_staticize_emoji_for_email'); add_filter( 'emoji_svg_url', create_function( '', 'return false;' ) );//禁用emoji预解析
根据上面的提示注释,我们把对应的代码添加到functions.php文件中,保存,上传,刷新网页,查看源代码,是不是简洁了许多。