一组代码优化WordPress头部代码去掉臃肿信息

我们是不是有看到在打开WordPress站点源代码看到头部乱七八糟的,很多代码其实是无效的,我们可以去掉后减轻体积提高网站速度和搜索引擎的抓取效率的。这里我们可以用到LESEO插件实现,或者也可以用到这样的代码。

//去除头部多余加载信息
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版本号、rel=canonical、dns-prefetch等等,去掉后再看看,没有影响的可以就这样,有印象的就去掉。


【AD】丽萨主机英国VPS推荐 英国原生IP双ISP家宅主机

【AD】美国洛杉矶/香港/日本VPS推荐,回程电信CN2 GIA线路,延迟低、稳定性高、免费备份_搬瓦工