如何无需插件优化WordPress速度(纯代码指南)

发表于 | 通过Siteskyline
6分钟阅读

If you’re reading this, you’re likely tired of the 标准的WordPress性能建议. The usual routine—installing WP Rocket, adding an image optimizer plugin, and throwing in an asset manager—often leads to a paradox: 你安装插件来加速网站,但插件本身却增加了数据库膨胀、后台定时任务以及自身CSS/JS负载。

The truth is, WordPress doesn’t need 15 performance plugins to load in under a second. True speed optimization happens at the metal layer—the server, the database, and the core code.

在本指南中,我们将完全绕过插件生态系统。我们将使用服务器端配置来优化WordPress, wp-config.php 调整和精确的 functions.php 代码片段。


1. 服务器端基础(无需PHP)

在触及一行WordPress代码之前,你的服务器环境必须得到充分优化。前端缓存无法修复一个弱服务器。

升级到PHP 8.1+

WordPress运行在PHP上。从PHP 7.4迁移到PHP 8.1或8.2可以减少20-30%的执行时间,并显著降低内存消耗。 行动: 在cPanel、Plesk中更改,或者如果你管理自己的VPS,通过CLI更改。

启用服务器级压缩(Brotli优于Gzip)

虽然Gzip是标准, Brotli (由Google开发)在相同的CPU成本下,为文本文件(HTML、CSS、JS)提供大约15-20%更好的压缩率。 操作(Nginx): 确保你的 nginx.conf 已启用 Brotli:

brotli on;
brotli_comp_level 6;
brotli_types text/plain text/css application/javascript application/json image/svg+xml;

实施 FastCGI 缓存(Nginx)或 LiteSpeed 缓存

不要使用基于 PHP 的缓存插件(这仍然需要 WordPress 部分加载以提供缓存),而是在 Web 服务器级别进行缓存。Nginx FastCGI 缓存将生成的 HTML 存储在 RAM 或磁盘中,并直接提供,完全绕过 PHP 和 MySQL 为匿名访客提供服务。


2. 通过以下方式加固和精简 wp-config.php

这 wp-config.php 文件是您的控制室。默认情况下,WordPress 允许某些行为,这些行为会随着时间的推移使数据库膨胀。

将这些代码片段添加到 /* That's all, stop editing! Happy publishing. */ 行之前。

限制文章修订

默认情况下,WordPress 会无限存储文章修订。一篇更新了 50 次的文章将会有 50 个副本存储在您的 wp_posts 数据库表中,这会大大减慢数据库查询速度。

// Keep only the last 3 revisions
define( 'WP_POST_REVISIONS', 3 );

优化自动保存间隔

WordPress 每 60 秒自动保存一次。如果有多个编辑同时工作,这会给数据库带来巨大压力。请降低频率。

// Change autosave from 60 seconds to 5 minutes
define( 'AUTOSAVE_INTERVAL', 300 );

更快清空回收站

已删除的文章和评论会在数据库中保留 30 天。缩短此时间以保持数据库精简。

// Empty trash every 7 days
define( 'EMPTY_TRASH_DAYS', 7 );

3. The “Anti-Bloat” functions.php 主代码片段

WordPress 核心会向您的 <head> 和 <footer> 默认情况下。

To achieve maximum information gain over competitors, we won’t just tell you to “use a debloat plugin.” Here is the exact code to surgically remove the most common WordPress bloat.

Note: Add this to your Child Theme’s functions.php 或一个 mu-plugin 文件中。

/**
 * The Ultimate WordPress Debloat Snippet
 */
add_action('init', function() {
    // 1. Remove RSD, XMLRPC, and WLW links
    remove_action('wp_head', 'rsd_link');
    remove_action('wp_head', 'wlwmanifest_link');
    add_filter('xmlrpc_enabled', '__return_false');

    // 2. Remove WordPress version footprint (Security & Speed)
    remove_action('wp_head', 'wp_generator');

    // 3. Remove Shortlinks and REST API links from header
    remove_action('wp_head', 'wp_shortlink_wp_head');
    remove_action('wp_head', 'rest_output_link_wp_head');

    // 4. Disable native Emojis (Saves 1 JS and 1 CSS HTTP Request)
    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');
    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');

    // 5. Disable oEmbeds (If you don't embed external URLs automatically)
    remove_action('wp_head', 'wp_oembed_add_discovery_links');
    remove_action('wp_head', 'wp_oembed_add_host_js');
    add_action('wp_footer', function() { wp_deregister_script('wp-embed'); });
});

/**
 * 6. Remove Global Styles (WP 5.9+ Inline CSS Bloat)
 */
add_action('wp_enqueue_scripts', function() {
    wp_dequeue_style('global-styles');
    wp_dequeue_style('classic-theme-styles');
}, 100);

/**
 * 7. Remove jQuery Migrate (If using modern themes/plugins)
 */
add_action('wp_default_scripts', function($scripts) {
    if (!is_admin() && isset($scripts->registered['jquery'])) {
        $script = $scripts->registered['jquery'];
        if ($script->deps) {
            $script->deps = array_diff($script->deps, ['jquery-migrate']);
        }
    }
});

/**
 * 8. Throttle the Heartbeat API
 * Prevents high CPU usage when leaving the WP Admin open
 */
add_filter('heartbeat_settings', function($settings) {
    $settings['interval'] = 60; // Throttle to 60 seconds
    return $settings;
});

我们刚刚实现了什么?

通过添加上述代码,您立即消除了多达 5-8 个不必要的 HTTP 请求 and removed dozens of lines of inline CSS/JS from every single page load—all without installing a single plugin.


4. 媒体优化(硬方法)

像 Smush 或 Imagify 这样的插件很方便,但它们会在您的 PHP 服务器上运行图像处理,消耗 CPU 资源。

手动 WebP/AVIF 转换

在上传任何图像到 WordPress 之前,通过外部的无损压缩工具如 Squoosh.app (由 Google 开发)或 TinyPNG。更好的是,将它们转换为 .webp 或 .avif 格式后再上传。

利用原生懒加载

自 WordPress 5.5 起,原生懒加载已内置。WordPress 会自动添加 loading="lazy" to your images. You don’t need a JavaScript-based lazy loading plugin. However, ensure your “above the fold” images (like your logo or hero image) DO NOT have this attribute, as it will delay the Largest Contentful Paint (LCP).

您可以通过以下方式移除页面中第一张图片的懒加载 functions.php:

add_filter( 'wp_get_attachment_image_attributes', function( $attr, $attachment, $size ) {
    static $is_first_image = true;
    if ( $is_first_image ) {
        $attr['loading'] = 'eager'; // Force immediate load for LCP
        $is_first_image = false;
    }
    return $attr;
}, 10, 3 );

5. 通过 phpMyAdmin 进行数据库碎片整理

Optimization plugins usually feature a “Clean Database” button. You can do this natively at the database level.

  1. 登录您的主机控制面板并打开 phpMyAdmin.
  2. 选择您的 WordPress 数据库。
  3. 滚动到底部,勾选 全选.
  4. In the “With selected:” dropdown, choose 优化表.

MySQL将重建表索引并回收未使用的空间。请每月手动执行一次。


裁决

Optimizing WordPress without plugins isn’t just about saving money on premium subscriptions. It is a philosophy of 减少技术债务. Every plugin you don’t install is a security vulnerability you don’t have to patch, a database table you don’t have to clean, and a PHP script your server doesn’t have to execute.

将工作负载转移到服务器(PHP 8.1+、Nginx FastCGI),在您的 wp-config.php,并使用您的 functions.php。您的首字节时间(TTFB)和核心网页指标将反映出差异。

Siteskyline

Siteskyline

Siteskyline is a premium WordPress plugin and SEO management platform dedicated to providing the best speed, security, and optimization tools.