说起 WordPress 主题,我们一般安装好一款 WordPress 主题后,查看源代码,都能看到其头部太多的信息,而这些信息很多都是无用的,但作为默认的根据 wp_head()这个钩子所加载着,着实有点费事,代码简洁控们就不喜欢了,如何去掉 WordPress 头部这些多余的 CSS、JS 的加载呢?我们下面具体说:
建议大家将这个 functions.php 文件通过 FTP 下载下来,先备份,后修改,因为一个不小心,少一个标点都会导致整站打不开!解决方法就是覆盖备份 functions.php 文件即可安然无恙!
直接打开您所使用主题文件里的 functions.php 文件,将以下代码贴进 functions.php 文件即可。刷新网页,查看源文件,看头部是否干净了许多?原本加载了很多的 js 是否都不见了??
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’ );
以上内容整理自@主题猫