移除 WordPress 头部和底部自带 js、css等代码声明的方法汇总

我们使用 WordPress建站后,自己查看网页源代码会发现 WP 自带了很多 js、css 等一些代码声明,特别是 WP 5.0+ 版本之后越来越多。

其实很多我们本地网络环境都用不到是可有可无的,这样不仅没有起到代码应有的作用还会影响网站网页的打开速度,得不偿失;特别是对有强迫症的用户更是欲去之而后快。

移除wordpress代码css、js

本文就尽量多的汇总了移除 WordPress 头部和底部自带 js、css等代码声明的各种方法以达到优化wordpress打开速度的目的,大家可以根据自己网站实际情况,按需使用即可。

以下代码的使用方法:在你 WordPress 网站当前使用主题的 funtions.php 文件中添加就好。

  1. /**
  2. * 移除底部js代码 Disable embeds
  3. */
  4. function disable_embeds_init(){
  5. global $wp;
  6. $wp->public_query_vars = array_diff($wp->public_query_vars, array('embed'));
  7. remove_action('rest_api_init', 'wp_oembed_register_route');
  8. add_filter('embed_oembed_discover', '__return_false');
  9. remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
  10. remove_action('wp_head', 'wp_oembed_add_discovery_links');
  11. remove_action('wp_head', 'wp_oembed_add_host_js');
  12. add_filter('tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin');
  13. add_filter('rewrite_rules_array', 'disable_embeds_rewrites');
  14. }
  15. add_action('init', 'disable_embeds_init', 9999);
  16. function disable_embeds_tiny_mce_plugin($plugins){
  17. return array_diff($plugins, array('wpembed'));
  18. }
  19. function disable_embeds_rewrites($rules){
  20. foreach ($rules as $rule => $rewrite) {
  21. if (false !== strpos($rewrite, 'embed=true')) {
  22. unset($rules[$rule]);
  23. }
  24. }
  25. return $rules;
  26. }
  27. function disable_embeds_remove_rewrite_rules(){
  28. add_filter('rewrite_rules_array', 'disable_embeds_rewrites');
  29. flush_rewrite_rules();
  30. }
  31. register_activation_hook(__FILE__, 'disable_embeds_remove_rewrite_rules');
  32. function disable_embeds_flush_rewrite_rules(){
  33. remove_filter('rewrite_rules_array', 'disable_embeds_rewrites');
  34. flush_rewrite_rules();
  35. }
  36. register_deactivation_hook(__FILE__, 'disable_embeds_flush_rewrite_rules');

移除 WordPress 头部加载 DNS 预获取(dns-prefetch)

  1. //移除 WordPress 头部加载 DNS 预获取(dns-prefetch)
  2. function remove_dns_prefetch( $hints, $relation_type ) {
  3. if ( 'dns-prefetch' === $relation_type ) {
  4. return array_diff( wp_dependencies_unique_hosts(), $hints );
  5. }
  6. return $hints;
  7. }
  8. add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );

移除 WordPress5.0添加的 block-library CSS

  1. //WordPress 5.0+移除 block-library CSS
  2. add_action( 'wp_enqueue_scripts', 'fanly_remove_block_library_css', 100 );
  3. function fanly_remove_block_library_css() {
  4. wp_dequeue_style( 'wp-block-library' );
  5. }

移除 wordpress顶部其他多余信息

  1. //移除顶部多余信息
  2. remove_action('wp_head', 'feed_links', 2); //文章和评论feed
  3. remove_action('wp_head', 'feed_links_extra', 3);// 额外的feed,例如category, tag页
  4. remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink
  5. remove_action('wp_head', 'rel_canonical' );
  6. remove_action('wp_head','rsd_link');//移除head中的rel="EditURI"
  7. remove_action('wp_head','wlwmanifest_link');//移除head中的rel="wlwmanifest"
  8. remove_action('template_redirect','wp_shortlink_header',11,0);//移除返回 HTTP 头中的 rel=shortlink
  9. remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇.
  10. remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );

禁用wordpress的 JSON REST API功能。

更多关于 WordPress禁用JSON REST API功能增加网站安全的说明

  1. //完全禁用wp-json
  2. function disable_rest_api( $access ) {
  3. return new WP_Error( '无访问权限', 'Soooooryyyy', array(
  4. 'status' => 403
  5. ) );
  6. }
  7. add_filter( 'rest_authentication_errors', 'disable_rest_api' );

还有去除顶部的调用REST API代码链接

  1. //移除顶部wp-json禁用REST API
  2. add_filter('json_enabled', '__return_false' );
  3. add_filter('json_jsonp_enabled', '__return_false' );
  4. add_filter('rest_enabled', '__return_false');
  5. add_filter('rest_jsonp_enabled', '__return_false');
  6. remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
  7. remove_action('template_redirect', 'rest_output_link_header', 11 );

还有禁用网站 Feed RSS 功能的代码:

  1. // 关闭 Feed RSS
  2. function jlove_disable_feed() {
  3. wp_die(__('<h1>Feed OFF 暂不提供 Feed 服务,请访问本网站<a href="'.get_bloginfo('url').'">首页</a>!</h1>'));
  4. }
  5. add_action('do_feed', 'jlove_disable_feed', 1);
  6. add_action('do_feed_rdf', 'jlove_disable_feed', 1);
  7. add_action('do_feed_rss', 'jlove_disable_feed', 1);
  8. add_action('do_feed_rss2', 'jlove_disable_feed', 1);
  9. add_action('do_feed_atom', 'jlove_disable_feed', 1);

如果加入上面的移除代码后,再到自己的 wordpress网站查看网页源代码,会发现头部少了很多代码声明,优化清爽整洁舒服多了!(本人强迫症患者O(∩_∩)O哈哈~)

可待后续更多......

 

转载请注明链接地址:荐爱小站 » 移除 WordPress 头部和底部自带 js、css等代码声明的方法汇总

赞 (5) 赏 !

觉得文章有用就打赏一下吧,赠人玫瑰手有余香!

支付宝扫一扫打赏

微信扫一扫打赏