説起 WordPress 主題,我們一般安裝好一款 WordPress 主題後,查看源代碼,都能看到其頭部太多的信息,而這些信息很多都是無用的,但作為默認的根據 wp_head() 這個鈎子所加載着,着實有點費事,代碼簡潔控們就不喜歡了,如何精簡 WordPress 頭部多餘信息,去掉部分不必要的 js 加載呢?我們下面具體説:
修改 WordPress 系統文件的方法大叔這裏就不説了,這樣不好,每次 WordPress 程序的更新,都得進行一次操作,費事!
下面直接打開您所使用主題的文件裏的 functions.php 吧,對,大家都喜歡將一些拓展函數放這個文件裏,方便!
大叔這裏還是建議大家將這個文件通過 FTP 下載下來,先備份,後修改,因為一個不小心,少一個標點都會導致整站打不開!解決方法就是覆蓋備份 functions.php 文件即可安然無恙!
下面就説重點了:
- remove_action( 'wp_head', 'wp_enqueue_scripts', 1 );
- remove_action( 'wp_head', 'feed_links', 2 );
- remove_action( 'wp_head', 'feed_links_extra', 3 );
- 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', 'locale_stylesheet' );
- remove_action('publish_future_post','check_and_publish_future_post',10, 1 );
- remove_action( 'wp_head', 'noindex', 1 );
- remove_action( 'wp_head', 'wp_print_styles', 8 );
- remove_action( 'wp_head', 'wp_print_head_scripts', 9 );
- remove_action( 'wp_head', 'wp_generator' );
- remove_action( 'wp_head', 'rel_canonical' );
- remove_action( 'wp_footer', 'wp_print_footer_scripts' );
- remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
- remove_action( 'template_redirect', 'wp_shortlink_header', 11, 0 );
- add_action('widgets_init', 'my_remove_recent_comments_style');
- function my_remove_recent_comments_style() {
- global $wp_widget_factory;
- remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
- }
- if ( !is_admin() ) {
- function my_init_method() {
- wp_deregister_script( 'jquery' );
- }
- add_action('init', 'my_init_method');
- }
- wp_deregister_script( 'l10n' );
將以上代碼貼進 functions.php 內,保存、上傳。。刷新網頁,查看源文件,看頭部是否乾淨了許多?原本加載了很多的 js 是否都不見了??呵呵,不用謝大叔,大叔叫雷鋒!