注意:WordPress加速的一个要点就是能不用插件就不要用插件,插件越多网站越慢。

方法

function.php文件一般在正在使用的主题根目录。

1、禁用谷歌字体
如果使用了WordPress默认的主题那么需要通过插件解决:Remove Open Sans font Link from WP core

如果是其他主题,添加:

/**
 * WordPress 后台禁用Google Open Sans字体,加速网站
 */
add_filter( 'gettext_with_context', 'wpdx_disable_open_sans', 888, 4 );
function wpdx_disable_open_sans( $translations, $text, $context, $domain ) {
  if ( 'Open Sans font: on or off' == $context && 'on' == $text ) {
    $translations = 'off';
  }
  return $translations;
}
function get_ssl_avatar($avatar) {
   $avatar = preg_replace('/.*\/avatar\/(.*)\?s=([\d]+)&.*/','*',$avatar); return $avatar; } add_filter('get_avatar', 'get_ssl_avatar'); ``` **3、强制jquery库文件底部载入** 将`JS`放到最后加载,有利于提高网站加载效率。 ``` //强制jquery库文件底部载入 function ds_print_jquery_in_footer( &$scripts) { if ( ! is_admin() ) $scripts->add_data( 'jquery', 'group', 1 ); } add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' ); ``` **4、去除加载的css和js后面的版本号** 去掉版本查询提高效率,看着也舒心。 ``` //去除加载的css和js后面的版本号 /** Remove Query strings from Static Resources. */ function _remove_script_version( $src ){ $parts = explode( '?', $src ); return $parts[0]; } add_filter( 'script_loader_src', '_remove_script_version', 15, 1 ); add_filter( 'style_loader_src', '_remove_script_version', 15, 1 ); add_filter( 'pre_option_link_manager_enabled', '__return_true' ); ``` **5、删除WP头不需要的代码** 这个去掉了,可以有效精简`WordPress`多余的`nearing`。 ``` //删除 wp_head 中无关紧要的代码 remove_action('wp_head', 'rsd_link'); remove_action('wp_head', 'wlwmanifest_link'); remove_action('wp_head', 'wp_generator'); remove_action('wp_head', 'start_post_rel_link'); remove_action('wp_head', 'index_rel_link'); remove_action('wp_head', 'adjacent_posts_rel_link'); ``` **6、禁用Emoji功能** `WordPress`的`EMOJI`图片国内无法使用,直接禁用。 ``` /* 禁用 Emoji 功能 */ remove_action('admin_print_scripts', 'print_emoji_detection_script'); remove_action('admin_print_styles', 'print_emoji_styles'); #remove_action('wp_head', 'print_emoji_detection_script', 7); remove_action('wp_print_styles', 'print_emoji_styles'); remove_action('embed_head', 'print_emoji_detection_script'); 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'); ``` **7、屏蔽文章Embed功能** 多余功能,去掉。 ``` remove_action('rest_api_init', 'wp_oembed_register_route'); remove_filter('rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4); remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10 ); remove_filter('oembed_response_data', 'get_oembed_response_data_rich', 10, 4); remove_action('wp_head', 'wp_oembed_add_discovery_links'); remove_action('wp_head', 'wp_oembed_add_host_js'); ``` **8、关闭XML-RPC功能** ``` // 关闭 XML-RPC 功能 add_filter('xmlrpc_enabled', '__return_false'); ``` **9、屏蔽REST API** ``` // 屏蔽 REST API add_filter('rest_enabled', '__return_false'); add_filter('rest_jsonp_enabled', '__return_false'); ``` **10、移除头部wp-json标签和HTTP header中的link** ``` //移除头部 wp-json 标签和 HTTP header 中的 link remove_action('wp_head', 'rest_output_link_wp_head', 10 ); remove_action('template_redirect', 'rest_output_link_header', 11 ); ``` **11、屏蔽若干无用功能** ``` // Disable auto-embeds for WordPress >= v3.5 remove_filter( 'the_content', array( $GLOBALS['wp_embed'], 'autoembed' ), 8 ); add_filter('automatic_updater_disabled', '__return_true'); // 彻底关闭自动更新 remove_action('init', 'wp_schedule_update_checks'); // 关闭更新检查定时作业 wp_clear_scheduled_hook('wp_version_check'); // 移除已有的版本检查定时作业 wp_clear_scheduled_hook('wp_update_plugins'); // 移除已有的插件更新定时作业 wp_clear_scheduled_hook('wp_update_themes'); // 移除已有的主题更新定时作业 wp_clear_scheduled_hook('wp_maybe_auto_update'); // 移除已有的自动更新定时作业 remove_action( 'admin_init', '_maybe_update_core' ); // 移除后台内核更新检查 remove_action( 'load-plugins.php', 'wp_update_plugins' ); // 移除后台插件更新检查 remove_action( 'load-update.php', 'wp_update_plugins' ); remove_action( 'load-update-core.php', 'wp_update_plugins' ); remove_action( 'admin_init', '_maybe_update_plugins' ); remove_action( 'load-themes.php', 'wp_update_themes' ); // 移除后台主题更新检查 remove_action( 'load-update.php', 'wp_update_themes' ); remove_action( 'load-update-core.php', 'wp_update_themes' ); remove_action( 'admin_init', '_maybe_update_themes' ); ``` 这些都完成后,我们会发现网站明显的变快了!版权声明:本文为原创文章,版权归 Rat's Blog 所有,转载请注明出处! `Vultr`新用户注册送`100`美元/`16`个机房按小时计费,支持支付宝,【点击查看】。*

    <div
最后修改:2026 年 07 月 15 日 05 : 03 AM
如果觉得我的文章对你有用,请随意赞赏