一组代码轻松解决WordPress头部无用JS/CSS/DNS加载(减轻页面体积)

WordPress程序的功能易用这个毋庸置疑的,而且用丰富的文档、主题和插件。但是唯独的就是代码特别臃肿,尤其是我们在没有处理之前,头部的代码一大堆,有些确实也没有用。我们有没有办法处理掉呢?比如我们可以用插件解决或者有些主题自带,实际上我们只要用到下面的代码就可以基本解决90%的问题。

//去除头部多余加载信息
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

根据上面的提示备注,我们可以清理WordPress头部的一些代码,减少体积。

试试看吧。

本文出处:老蒋部落 » 一组代码轻松解决WordPress头部无用JS/CSS/DNS加载(减轻页面体积) | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送