// ===== 性能优化 =====
// 1. 给正文和列表页图片加 loading=lazy(跳过首屏logo和banner)
add_filter('the_content', function($content) {
// 给正文img加lazy(已有loading属性的跳过)
$content = preg_replace(
'/]*)>/i',
'
',
$content
);
return $content;
}, 99);
add_filter('post_thumbnail_html', function($html) {
$html = preg_replace(
'/
]*)>/i',
'
',
$html
);
return $html;
}, 99);
// 2. 给非关键JS加defer(排除jquery)
add_filter('script_loader_tag', function($tag, $handle) {
$exclude = ['jquery', 'jquery-core', 'jquery-migrate'];
if (in_array($handle, $exclude)) return $tag;
if (strpos($tag, 'defer') !== false) return $tag;
return str_replace(' src=', ' defer src=', $tag);
}, 10, 2);
// 3. 移除多余的wp-embed脚本
add_action('wp_footer', function() {
wp_deregister_script('wp-embed');
});