WordPress移除head头部js、css、feed等多余加载项
在我们开发WordPress主题时,细心的小伙伴或发现网站头部如果加载head页面就会出现很多系统自带的加载项目,例如自带的css、js、feed、style等多余信息。
这些加载项目很多是没有必要加载的,那么我们如何将这些多余的head头部信息移除呢?
方法很简单,网上针对此类WordPress优化的教程也很多,今天小白学习资源站就给大家整理下。
我们先看看移除多余加载项之前的代码截图

解决方法
//去除头部多余加载信息
remove_action( \'wp_head\', \'wp_generator\' );//移除WordPress版本
remove_action( \'wp_head\', \'rsd_link\' );//移除离线编辑器开放接口
remove_action( \'wp_head\', \'wlwmanifest_link\' );//移除离线编辑器开放接口
remove_action( \'wp_head\', \'index_rel_link\' );//去除本页唯一链接信息
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\', \'feed_links\', 2 );//移除文章和评论feed
remove_action( \'wp_head\', \'feed_links_extra\', 3 ); //移除分类等feed
remove_action( \'wp_head\', \'rest_output_link_wp_head\', 10 ); //移除wp-json
remove_action( \'wp_head\', \'print_emoji_detection_script\', 7 ); //头部的JS代码
add_filter( \'show_admin_bar\', \'__return_false\' );//移除wp-json链接
remove_action( \'wp_head\', \'rel_canonical\' ); //rel=canonical
remove_action( \'wp_head\', \'wp_shortlink_wp_head\', 10, 0 ); //rel=shortlink
//remove_action( \'wp_head\', \'wp_print_styles\', 8 ); //移除后台插件加载css
remove_action( \'wp_head\', \'print_emoji_detection_script\', 7 );//移除emoji载入js
remove_action( \'admin_print_scripts\', \'print_emoji_detection_script\' );//emoji载入js
remove_action( \'wp_print_styles\', \'print_emoji_styles\' );//移除emoji载入css
remove_action( \'admin_print_styles\', \'print_emoji_styles\' );
remove_action( \'wp_head\', \'rest_output_link_wp_head\', 10 );
remove_action(\'wp_head\',\'wp_resource_hints\',2);//移除dns-prefetch
使用方法
将上面代码添加到主题的functions.php文件中, 保存后在看看网站的代码。
head头部是不是少了很多多余加载项信息?
下面是精简后的代码截图

文章来自小白学习资源站https://btzy.cc转载请注明出处,并保留本链接,谢谢!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。源码素材属于虚拟商品,具有可复制性,可传播性,一旦授予,不接受任何形式的退款、换货要求。请您在购买获取之前确认好 是您所需要的资源
1.本站提供的一切源码、教程和内容信息仅限用于学习和研究目的;不得将上述内容用于商业或者非法用途!
2.本站信息来自网络收集整理,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑或手机中彻底删除上述内容!
3.本站提供的源码,模板,插件等其他资源都不包含技术服务,敬请谅解!
4.本站资源售价只是赞助,收取费用仅维持本站日常运营所需!
5.如无备注,本站不保证所提供下载的资源准确性安全性和完整性,源码仅供下载学习之用!
6.如用于商业或者非法用途,与本站无关。一切后果请自行负责!
7.如果遇到加密压缩包,默认解压密码为:btzy.cc或bori99.com如遇到无法解压请联系站长!