WordPress优化篇—移除不需要的代码

目录 Wordpress相关2017年4月15日

Wodpress有我们许多用不到的功能代码,这些代码还有可能造成漏洞、拖慢博客,我们只需要在主题中添加一些代码,即可屏蔽这些对我们没有意义的代码。主要是优化网页头部的冗余代码,屏蔽文章Embed功能,禁用Rest api,移除emoji表情代码。

以下代码只需添加到主题的function.php文件中即可。代码均收集于网络,本人整理汇总。

WP精简头部代码

wp-head()函数在网页的头部引入了大量对我们没有多大意义的代码,根据注释选择你需要移除的代码。

  1. //去除头部冗余代码   
  2.     remove_action('wp_head', 'feed_links', 2 ); //移除文章和评论feed
  3.     remove_action('wp_head', 'feed_links_extra', 3); //移除分类等feed
  4.     remove_action('wp_head', 'rsd_link'); //移除离线编辑器开放接口
  5.     remove_action('wp_head', 'wlwmanifest_link'); //移除离线编辑器开放接口
  6.     remove_action('wp_head', 'index_rel_link'); //移除当前页面的索引
  7.     remove_action('wp_head', 'start_post_rel_link', 10, 0); //移除最开始文章的url
  8.     remove_action('wp_head', 'parent_post_rel_link', 10, 0 ); //移除后面文章的url
  9.     remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); //移除相邻文章的url
  10.     remove_action('wp_head', 'wp_generator'); //隐藏wordpress版本
  11.     remove_action('wp_head', 'rel_canonical'); //去除默认canonical标签
  12.     remove_action('wp_head','wp_shortlink_wp_head',10,0); //去除shortlink短链接
  13.     remove_action('template_redirect','wp_shortlink_header',11,0); //去除shortlink短链接

移除emoji表情代码

wordpress4.2版本之后会自动在加载一段用于支持emjo表情的脚本(JS+CSS),但对于大多数人来说没有多大用,并且会拖慢博客速度,用下面的代码即可移除emoji表情代码。

  1. //移除emoji表情代码   
  2. function disable_emojis() {   
  3.      remove_action( 'wp_head', 'print_emoji_detection_script', 7 );   
  4.      remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );   
  5.      remove_action( 'wp_print_styles', 'print_emoji_styles' );   
  6.      remove_action( 'admin_print_styles', 'print_emoji_styles' );    
  7.      remove_filter( 'the_content_feed', 'wp_staticize_emoji' );   
  8.      remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );    
  9.      remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );   
  10.      add_filter( 'tiny_mce_plugins', 'disable_emojis_tinymce' );   
  11.     }   
  12. add_action( 'init', 'disable_emojis' );   
  13. /**  
  14.      * Filter function used to remove the tinymce emoji plugin.  
  15.      *   
  16.      * @param    array  $plugins    
  17.      * @return   array             Difference betwen the two arrays  
  18.      */  
  19. function disable_emojis_tinymce( $plugins ) {   
  20.      if ( is_array$plugins ) ) {
  21.      return array_diff$pluginsarray( 'wpemoji' ) );
  22.      } else {
  23.      return array();
  24.      }   
  25.     }  

屏蔽文章Embed功能

WordPress 4.4 增强了 Easy Embeds 功能,发布了一个新功能叫做 Post Embed,可以在任意 WordPress 站点用嵌入的方式插入 WordPress 文章。如果觉得这玩意对你没什么意义,你也可以屏蔽它。

  1. remove_action('rest_api_init', 'wp_oembed_register_route');   
  2. remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4);   
  3. remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);   
  4. remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4);   
  5. remove_action('wp_head', 'wp_oembed_add_discovery_links');   
  6. remove_action('wp_head', 'wp_oembed_add_host_js');

禁用REST API

WordPress4.4开始增加REST API功能, 对于一般的网站是没有需要的,反而会拖累网站的速度,所以我们尽可能的禁止掉这些不必要的功能需求。

  1. // 屏蔽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. // 移除头部wp-json标签和HTTP header中的link   
  7. remove_action('wp_head', 'rest_output_link_wp_head', 10 );   
  8. remove_action('template_redirect', 'rest_output_link_header', 11 );  

禁止英文半角转换成中文全角

禁止字符转义的所有代码如下,你可以根据自己的需要,选自己要的代码,添加到主题的 functions.php 文件:

  1. // 取消转义 Quotmarks Replacer   
  2. $qmr_work_tags = array(   
  3.   'the_title',             // http://codex.wordpress.org/Function_Reference/the_title
  4.   'the_content',           // http://codex.wordpress.org/Function_Reference/the_content
  5.   'the_excerpt',           // http://codex.wordpress.org/Function_Reference/the_excerpt
  6.   // 'list_cats',          Deprecated. http://codex.wordpress.org/Function_Reference/list_cats
  7.   'single_post_title',     // http://codex.wordpress.org/Function_Reference/single_post_title
  8.   'comment_author',        // http://codex.wordpress.org/Function_Reference/comment_author
  9.   'comment_text',          // http://codex.wordpress.org/Function_Reference/comment_text
  10.   // 'link_name',          Deprecated.
  11.   // 'link_notes',         Deprecated.
  12.   'link_description',      // Deprecated, but still widely used.
  13.   'bloginfo',              // http://codex.wordpress.org/Function_Reference/bloginfo
  14.   'wp_title',              // http://codex.wordpress.org/Function_Reference/wp_title
  15.   'term_description',      // http://codex.wordpress.org/Function_Reference/term_description
  16.   'category_description',  // http://codex.wordpress.org/Function_Reference/category_description
  17.   'widget_title',          // Used by all widgets in themes
  18.   'widget_text'            // Used by all widgets in themes
  19.   );   
  20. foreach ( $qmr_work_tags as $qmr_work_tag ) {   
  21.   remove_filter ($qmr_work_tag, 'wptexturize');
  22. }  

还可以使用下面的形式:

  1. //取消内容转义    
  2. remove_filter('the_content', 'wptexturize');
  3. //取消摘要转义   
  4. remove_filter('the_excerpt', 'wptexturize');   
  5. //取消评论转义    
  6. remove_filter('comment_text', 'wptexturize');  

暂无评论

发表评论