說起 WordPress 主題,我們一般安裝好一款 WordPress 主題後,檢視原始碼,都能看到其頭部太多的資訊,而這些資訊很多都是無用的,但作為預設的根據 wp_head() 這個鉤子所載入著,著實有點費事,程式碼簡潔控們就不喜歡了,如何精簡 WordPress 頭部多餘資訊,去掉部分不必要的 js 載入呢?我們下面具體說:

修改 WordPress 系統檔案的方法大叔這裡就不說了,這樣不好,每次 WordPress 程式的更新,都得進行一次操作,費事!

下面直接開啟您所使用主題的檔案裡的 functions.php 吧,對,大家都喜歡將一些拓展函式放這個檔案裡,方便!

大叔這裡還是建議大家將這個檔案透過 FTP 下載下來,先備份,後修改,因為一個不小心,少一個標點都會導致整站打不開!解決方法就是覆蓋備份 functions.php 檔案即可安然無恙!

下面就說重點了:

  1. remove_action( 'wp_head', 'wp_enqueue_scripts', 1 ); 
  2. remove_action( 'wp_head', 'feed_links', 2 ); 
  3. remove_action( 'wp_head', 'feed_links_extra', 3 ); 
  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', 'parent_post_rel_link', 10, 0 );
  8. remove_action('wp_head', 'start_post_rel_link', 10, 0 );
  9. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
  10. remove_action( 'wp_head', 'locale_stylesheet' );
  11. remove_action('publish_future_post','check_and_publish_future_post',10, 1 );
  12. remove_action( 'wp_head', 'noindex', 1 );
  13. remove_action( 'wp_head', 'wp_print_styles', 8 );
  14. remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
  15. remove_action( 'wp_head', 'wp_generator' ); 
  16. remove_action( 'wp_head', 'rel_canonical' );
  17. remove_action( 'wp_footer', 'wp_print_footer_scripts' );
  18. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
  19. remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
  20. add_action('widgets_init', 'my_remove_recent_comments_style');
  21. function my_remove_recent_comments_style() {
  22. global $wp_widget_factory;
  23. remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
  24. }
  1. if ( !is_admin() ) { 
  2. function my_init_method() {
  3. wp_deregister_script( 'jquery' ); 
  4. }
  5. add_action('init', 'my_init_method');
  6. }
  7. wp_deregister_script( 'l10n' );

將以上程式碼貼進 functions.php 內,儲存、上傳。。重新整理網頁,檢視原始檔,看頭部是否乾淨了許多?原本載入了很多的 js 是否都不見了??呵呵,不用謝大叔,大叔叫雷鋒!