如图,直接上插件,加速你的 wordpress 网站,直接安装好后,自动屏蔽谷歌字体等。非常丝滑
<?php
/*
Plugin Name: WP Speed Up China
Plugin URI: https://www.kejianet.cn
Description: 专为 WP 4.2 + PHP 7.0 定制:拦截 Google Fonts;禁用头像/Emoji/XML-RPC/评论/Feed;清理头部;限制修订与心跳。
Version: 2.3.0
Author: QianYa
Author URI: https://www.kejianet.cn
License: GPL2
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* =========================================================
* 1) 字体与静态资源净化 (针对 WP 4.2 极简处理)
* =========================================================
*/
// 辅助函数:检测 Google Fonts
function wsuc_is_google_fonts( $url ) {
return ( is_string( $url ) && ( strpos( $url, 'fonts.googleapis.com' ) !== false || strpos( $url, 'fonts.gstatic.com' ) !== false ) );
}
/**
* 1.1 队列层面移除
*/
function wsuc_dequeue_google_fonts() {
global $wp_styles;
if ( ! is_a( $wp_styles, 'WP_Styles' ) || empty( $wp_styles->registered ) ) return;
foreach ( $wp_styles->registered as $handle => $style ) {
if ( isset( $style->src ) && wsuc_is_google_fonts( $style->src ) ) {
wp_dequeue_style( $handle );
wp_deregister_style( $handle );
}
}
}
// 挂载到所有可能的加载点
add_action( 'wp_enqueue_scripts', 'wsuc_dequeue_google_fonts', 999 );
add_action( 'admin_enqueue_scripts', 'wsuc_dequeue_google_fonts', 999 );
add_action( 'login_enqueue_scripts', 'wsuc_dequeue_google_fonts', 999 );
/**
* 1.2 拦截 <link> 标签输出 (兼容 WP 4.2 参数)
*/
function wsuc_clean_style_tag( $html, $handle, $href = null, $media = null ) {
if ( wsuc_is_google_fonts( $href ) || wsuc_is_google_fonts( $html ) ) {
return '';
}
return $html;
}
add_filter( 'style_loader_tag', 'wsuc_clean_style_tag', 10, 4 );
/**
* 1.3 拦截 src 属性 & 移除 ?ver= 参数
*/
function wsuc_clean_loader_src( $src, $handle ) {
// 拦截字体
if ( wsuc_is_google_fonts( $src ) ) return '';
// 仅前台移除 ver 参数
if ( ! is_admin() && strpos( $src, 'ver=' ) !== false ) {
$src = remove_query_arg( 'ver', $src );
}
return $src;
}
add_filter( 'style_loader_src', 'wsuc_clean_loader_src', 10, 2 );
add_filter( 'script_loader_src', 'wsuc_clean_loader_src', 10, 2 );
/**
* =========================================================
* 2) 移除 Emoji 与 Gravatar (WP 4.2 核心痛点)
* =========================================================
*/
function wsuc_remove_emojis() {
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );
remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
add_filter( 'tiny_mce_plugins', function( $plugins ) {
return is_array( $plugins ) ? array_diff( $plugins, array( 'wpemoji' ) ) : array();
});
}
add_action( 'init', 'wsuc_remove_emojis' );
// 禁用头像,避免连接 gravatar.com 超时
add_filter( 'option_show_avatars', '__return_false' );
/**
* =========================================================
* 3) 数据库与心跳优化
* =========================================================
*/
// 保留最近 3 个修订版本
add_filter( 'wp_revisions_to_keep', function( $num, $post ) { return 3; }, 10, 2 );
// 心跳降频 (60秒)
add_filter( 'heartbeat_settings', function( $settings ) {
$settings['interval'] = 60;
return $settings;
});
/**
* =========================================================
* 4) 安全防护与头部清理
* =========================================================
*/
// 禁用 XML-RPC (防止暴力破解和 DDoS)
add_filter( 'xmlrpc_enabled', '__return_false' );
add_filter( 'xmlrpc_methods', '__return_empty_array' );
// 移除头部多余链接
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' ); // 移除版本号
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 ); // 移除上下篇链接(可选,节省查询)
// 移除 Pingback Header
add_filter( 'wp_headers', function( $headers ) {
if ( isset( $headers['X-Pingback'] ) ) unset( $headers['X-Pingback'] );
return $headers;
});
add_filter( 'bloginfo_url', function( $output, $show ) {
return ( $show === 'pingback_url' ) ? '' : $output;
}, 10, 2 );
/**
* =========================================================
* 5) 禁用 Feed (彻底切断)
* =========================================================
*/
function wsuc_kill_feed() {
if ( ! is_admin() ) {
wp_die( 'No Feed', '', array( 'response' => 403 ) );
}
}
add_action( 'do_feed', 'wsuc_kill_feed', 1 );
add_action( 'do_feed_rdf', 'wsuc_kill_feed', 1 );
add_action( 'do_feed_rss', 'wsuc_kill_feed', 1 );
add_action( 'do_feed_rss2', 'wsuc_kill_feed', 1 );
add_action( 'do_feed_atom', 'wsuc_kill_feed', 1 );
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
/**
* =========================================================
* 6) 界面净化 (登录页 & 后台)
* =========================================================
*/
// 移除后台左上角 LOGO
add_action( 'admin_bar_menu', function( $wp_admin_bar ) {
$wp_admin_bar->remove_node( 'wp-logo' );
$wp_admin_bar->remove_node( 'comments' );
}, 999 );
// 移除后台页脚文字
add_filter( 'admin_footer_text', '__return_empty_string' );
add_filter( 'update_footer', '__return_empty_string', 11 );
// 登录页去 Logo
add_action( 'login_enqueue_scripts', function() {
echo '<style>.login h1 a { display:none !important; }</style>';
});
// 移除评论菜单
add_action( 'admin_menu', function() {
remove_menu_page( 'edit-comments.php' );
});
// 关闭评论功能
add_filter( 'comments_open', '__return_false', 20, 2 );
add_filter( 'pings_open', '__return_false', 20, 2 );
/**
* =========================================================
* 7) 轻量级 404 拦截 (针对 favicon/apple-touch-icon)
* =========================================================
*/
add_action( 'init', function() {
if ( is_admin() || empty( $_SERVER['REQUEST_URI'] ) ) return;
// 只检测最常见的缺失文件
$uri = $_SERVER['REQUEST_URI'];
if ( strpos( $uri, 'favicon.ico' ) !== false || strpos( $uri, 'apple-touch-icon' ) !== false ) {
// 尝试寻找真实文件
$rel_path = parse_url( $uri, PHP_URL_PATH );
if ( file_exists( ABSPATH . ltrim( $rel_path, '/' ) ) ) return;
// 无文件直接返回 204,节省资源
header( 'HTTP/1.1 204 No Content' );
header( 'Cache-Control: public, max-age=86400' );
exit;
}
}, 0 );
wordpress 优化插件下载地址:dt-the7_v.4.4.4 WP-Speed-Up-China





