wp_head() 是 WordPress 最重要的钩子函数之一,但也是页面加载性能的”重灾区”。通过优化 wp_head(),可以显著提升网站加载速度,改善用户体验和SEO表现。
一、了解 wp_head() 的”负担”
在主题的 header.php 文件中,wp_head() 函数会输出以下内容:
- 版本信息 – WordPress 版本、主题版本
- RSS 源链接 – 多种 RSS 订阅地址
- 样式表链接 – 核心CSS、主题CSS、插件CSS
- JavaScript 文件 – 核心JS、插件JS
- 额外元标签 – 各种插件添加的元信息
- 短链接 – 文章短链接
- emoji 脚本 – 表情符号支持脚本
- dns-prefetch 链接 – DNS 预获取
- 各种插件添加的代码
二、清理不必要的头部代码
1. 移除 WordPress 版本号
remove_action('wp_head', 'wp_generator');
2. 移除短链接
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
3. 移除 Windows Live Writer 支持
remove_action('wp_head', 'wlwmanifest_link');
4. 移除文章编辑链接
remove_action('wp_head', 'index_rel_link');
remove_action('wp_head', 'start_post_rel_link', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
5. 移除 Feed 链接(如果不需要)
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
6. 禁止 Emoji 表情
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_scripts', 'print_emoji_detection_script');
remove_action('admin_print_styles', 'print_emoji_styles');
三、优化样式和脚本加载
1. 延迟非关键 JavaScript
// 在 functions.php 中添加
function defer_parsing_of_js($url) {
if (is_admin()) return $url;
if (FALSE === strpos($url, '.js')) return $url;
if (strpos($url, 'jquery.js')) return $url;
return str_replace(' src', ' defer src', $url);
}
add_filter('script_loader_tag', 'defer_parsing_of_js', 10);
2. 异步加载脚本
function async_scripts($tag, $handle, $src) {
$async_scripts = array('ga-script', 'fb-script'); // 需要异步的脚本句柄
if (in_array($handle, $async_scripts)) {
return '<script src="' . $src . '" async></script>' . "\n";
}
return $tag;
}
add_filter('script_loader_tag', 'async_scripts', 10, 3);
3. 移除不必要的样式表
// 移除不使用的插件样式
function remove_unused_styles() {
wp_dequeue_style('plugin-style-handle'); // 替换为要移除的样式句柄
wp_deregister_style('plugin-style-handle');
}
add_action('wp_enqueue_scripts', 'remove_unused_styles', 100);
四、DNS 预连接优化
移除默认的 dns-prefetch
remove_action('wp_head', 'wp_resource_hints', 2);
添加自定义的 DNS 预连接
function custom_dns_prefetch() {
echo '<link rel="dns-prefetch" href="//fonts.googleapis.com">' . "\n";
echo '<link rel="dns-prefetch" href="//www.google-analytics.com">' . "\n";
echo '<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>' . "\n";
}
add_action('wp_head', 'custom_dns_prefetch', 0);
五、清理插件添加的头部代码
1. 识别插件添加的代码
使用以下插件查看头部输出:
- Query Monitor
- Show Current Template
2. 移除特定插件代码
// 移除特定插件添加的代码
function remove_plugin_header_code() {
// Yoast SEO
if (class_exists('WPSEO_Frontend')) {
global $wpseo_front;
remove_action('wp_head', array($wpseo_front, 'head'), 1);
}
// Contact Form 7
add_filter('wpcf7_load_js', '__return_false');
add_filter('wpcf7_load_css', '__return_false');
}
add_action('init', 'remove_plugin_header_code');
六、优化后的完整示例
// 在 functions.php 中添加
function optimize_wp_head() {
// 清理头部
remove_action('wp_head', 'wp_generator');
remove_action('wp_head', 'wlwmanifest_link');
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
// 移除emoji
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');
// 移除dns-prefetch
remove_action('wp_head', 'wp_resource_hints', 2);
// 移除jQuery Migrate
add_action('wp_default_scripts', function($scripts) {
if (!is_admin() && !empty($scripts->registered['jquery'])) {
$scripts->registered['jquery']->deps = array_diff(
$scripts->registered['jquery']->deps, ['jquery-migrate']
);
}
});
}
add_action('init', 'optimize_wp_head');
七、性能测试和验证
1. 优化前测试
- 使用 GTmetrix
- 使用 Google PageSpeed Insights
- 使用 Pingdom Tools
2. 优化后测试对比
- 页面大小减少
- 请求数量减少
- 加载时间缩短
- 性能评分提升
八、注意事项
- 谨慎操作:删除前确认功能是否真的不需要
- 备份主题文件:修改 functions.php 前做好备份
- 插件冲突:某些优化可能与特定插件冲突
- 逐步测试:每做一项优化都要测试网站功能
- 使用子主题:所有修改在子主题中进行
九、推荐优化插件
- WP Rocket – 综合性能优化
- Autoptimize – 代码压缩优化
- Perfmatters – 精细化控制wp_head()
- Asset CleanUp – 按需加载资源
结论
通过系统化优化 wp_head(),可以有效减少 HTTP 请求,压缩页面体积,提升加载速度。建议从最简单的清理开始,逐步深入,每步都进行测试验证。一个”瘦身”成功的头部不仅能提升用户体验,还能在搜索引擎排名中获得优势。
优化目标:将头部代码从 2-5KB 减少到 1KB 以内,HTTP 请求减少 30-50%,页面加载速度提升 20-40%。
记住:最好的优化是最适合你网站的优化,不要盲目追求极限而牺牲了必要的功能。


湘公网安备43020002000238