Wordpress基礎:精簡頭部wp_head


在Wordpress里

<?php wp_head(); ?>

wp_head()是一個重要的函數,它允許插件開發者向你的站點動態地添加CSS和javascript,如果我們不在模板中引入這個,一個插件將不能工作。

雖然有很多我們不需要的東西,但我們應該學會如何精簡,而不是簡單粗暴的刪除,下面可以通過remove_action移除不要的代碼。

把對應代碼放到functions.php

<?php
//移除頂部多余信息
remove_action('wp_head', 'index_rel_link');//當前文章的索引
remove_action('wp_head', 'feed_links_extra', 3);// 額外的feed,例如category, tag頁
remove_action('wp_head', 'start_post_rel_link', 10, 0);// 開始篇
remove_action('wp_head', 'parent_post_rel_link', 10, 0);// 父篇
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); // 上、下篇.
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );//rel=pre
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0 );//rel=shortlink
remove_action('wp_head', 'rel_canonical' );
wp_deregister_script('l10n');
remove_action('wp_head','rsd_link');//移除head中的rel="EditURI"
remove_action('wp_head','wlwmanifest_link');//移除head中的rel="wlwmanifest"
remove_action('wp_head','rsd_link');//rsd_link移除XML-RPC
remove_filter('the_content', 'wptexturize');//禁用半角符號自動轉換為全角
remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'], 'recent_comments_style'));&nbsp;
}
?>

打開 wp-includes 文件夾下的 default-filters.php 文件。找到如下代碼,對需要刪除的內容進行注釋即可。

add_action( 'wp_head',             '_wp_render_title_tag',            1     );
add_action( 'wp_head',             'wp_enqueue_scripts',              1     );
add_action( 'wp_head',             'feed_links',                      2     );
add_action( 'wp_head',             'feed_links_extra',                3     );
add_action( 'wp_head',             'rsd_link'                               );
add_action( 'wp_head',             'wlwmanifest_link'                       );
add_action( 'wp_head',             'adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head',             'locale_stylesheet'                      );
add_action( 'publish_future_post', 'check_and_publish_future_post',   10, 1 );
add_action( 'wp_head',             'noindex',                          1    );
add_action( 'wp_head',             'print_emoji_detection_script',     7    );
add_action( 'wp_head',             'wp_print_styles',                  8    );
add_action( 'wp_head',             'wp_print_head_scripts',            9    );
add_action( 'wp_head',             'wp_generator'                           );
add_action( 'wp_head',             'rel_canonical'                          );
add_action( 'wp_head',             'wp_shortlink_wp_head',            10, 0 );
add_action( 'wp_head',             'wp_site_icon',                    99    );
add_action( 'wp_footer',           'wp_print_footer_scripts',         20    );
...

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM