Products
GG网络技术分享 2025-03-18 16:12 0
用WordPress建站,方是方便,但由于WordPress是一个国际化的软件,在网页头部代码里会产生很多符合国际标准的标签(如果你用了国人设计的模板,此现象不易出现),这些标签每个都有自己的意义和用途,但在中国的互联网环境下,则有很多是用不到的。如果在网站页面里加载太多的标签,会影响页面的加载速度,如果确定无用的,手狠一点,该删除且删除。
那到底哪些头部标签可以删除呢?华哥从自己的几个网站中,找出大部分在用的。
去除WORDPRESS自带的 Emoji
// WordPress Emoji Delete
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预解析
去除 XMLRPC, WLW, Generator, Feeds 和 ShortLink
去掉这些标记,向 function.php(在你所使用的模板主题目录下) 文件里添加下面的代码:
remove_action(\'wp_head\', \'rsd_link\'); //removes EditURI/RSD (Really Simple Discovery) link.
remove_action(\'wp_head\', \'wlwmanifest_link\'); //removes wlwmanifest (Windows Live Writer) link.
remove_action(\'wp_head\', \'wp_generator\'); //removes meta name generator.
remove_action(\'wp_head\', \'wp_shortlink_wp_head\'); //removes shortlink.
remove_action( \'wp_head\', \'feed_links\', 2 ); //removes feed links.
remove_action(\'wp_head\', \'feed_links_extra\', 3 ); //removes comments feed.
去除 Previous 和 Next 文章链接
去掉文章的上一篇和下一篇链接,向 function.php 文件里添加下面的代码:
/*Removes prev and next links*/
remove_action(\'wp_head\', \'adjacent_posts_rel_link_wp_head\');
去除 XFN (XHTML Friends Network) Profile 链接 和 Pingback URL
这个 rel=profile 链接和 rel=Pingback 标记可以从header.php文件里直接删除。做法是,打开Wordperss主题里的header.php文件,删除下面两行:
<link rel=\"profile\" href=\"http://gmpg.org/xfn/11\">
<link rel=\"pingback\" href=\"<?php bloginfo( \'pingback_url\' ); ?>\">
禁用REST API功能
//禁用REST API功能代码
add_filter(\'rest_enabled\', \'__return_false\');
add_filter(\'rest_jsonp_enabled\', \'__return_false\');
移除wp-json链接
//移除wp-json链接的代码
remove_action( \'wp_head\', \'rest_output_link_wp_head\', 10 );
remove_action( \'wp_head\', \'wp_oembed_add_discovery_links\', 10 );
根据上边的内容,对比检查自己WORDPRESS网站首页源代码里的头部标签(head区),有选择地删除即可。
WordPress安装后默认会在主题的区域内加上很多无多大意义的代码,主要包括feed订阅地址、文章短地址、WordPress版本、父页开始页文章地址等等。这些默认添加到主题头部中的无用代码有些是对SEO有不好影响的,有些是无意义的代码。删除或者说禁用这些代码基本是安装WordPress系统后最先要做的事。删除的方法很简单,只需将以下代码插入你主题的function.php文件保存即可。
/*
删除WordPress无用头部代码
代码来源: www.wpzxbj.com
*/
remove_action( \'wp_head\', \'feed_links_extra\', 3 ); // 删除feed
remove_action( \'wp_head\', \'feed_links\', 2 ); // 删除文章评论feed
remove_action( \'wp_head\', \'rsd_link\'); // 删除RSD
remove_action( \'wp_head\', \'wlwmanifest_link\'); // 删除Windows Live Writer
remove_action( \'wp_head\', \'index_rel_link\'); // 删除index链接
remove_action( \'wp_head\', \'parent_post_rel_link\', 10, 0 ); // 删除父页
remove_action( \'wp_head\', \'start_post_rel_link\', 10, 0 ); // 删除开始页
remove_action( \'wp_head\', \'adjacent_posts_rel_link_wp_head\', 10, 0 ); // 删除文章相关链接
remove_action( \'wp_head\', \'wp_generator\'); // 删除版本
remove_action( \'wp_head\', \'wp_shortlink_wp_head\', 10, 0 ); // 删除文章短地址
remove_action( \'template_redirect\', \'wp_shortlink_header\', 11, 0 ); // 删除跳转
这里需要注意的是,有些SEO插件已提供删除这些无用代码的功能。比如wp seo插件就可以删除部分代码,如果你已用以上方法删除无用代码了,就不要开启SEO插件的类似功能,以免造成可能的程序故障而打不开网站前台页面。
Demand feedback