让你的WP跑得更快 - WordPress优化指南
本文最后更新于 922 天前,其中的信息可能已经有所发展或是发生改变。

WordPress

WordPress作为最流行的开源博客系统,2019年市场份额已经达到了33.4%,市场占有率达到三分之一。同时也有很多小伙伴将WP作为自己的博客系统(而我并没有),那么如何让自己的博客跑得更快呢?这里有关于优化的一点点小心得,希望能对你有帮助。

程序优化

插件与主题的使用

WordPress拥有优秀的的兼容性与生态,因此网络上存在大量的主题与插件供我们选择,我们在选择插件时应该遵循几个原则:

  • 只选有用的插件:用处不大的插件只会拖慢博客的运行速度,不需要的插件在后台禁用或删除。
  • 尽量选择正版插件,不要使用未知来源的插件
  • 同类插件不需要安装多个

对于主题,也是相同的道理。如果没有必要,尽量不要使用国外的主题,因为它们可能会引用大量国外的静态资源,拖慢加载速度。国内的主题对于相应文件都会有一定优化,提升加载速度。

启用缓存

WordPress有多种缓存机制,合理使用缓存,能让站点速度大大提升。

  • 使用 WP Rocket / WP Super Cache / W3 Total Cache 等缓存插件
  • 启用 Memcached / Redis 等配合相关 Object Cache(进阶)
  • 使用 LiteSpeed 等(进阶)

修改配置

WordPress中许多不必要的功能可以通过修改配置文件的方式关闭。注意:在进行相关操作前务必备份好源文件!

  • wp-config.php文件后添加下列内容
//WordPress自定义优化项。
define('WP_DEBUG', false);  //关闭DEBUG
define('AUTOSAVE_INTERVAL', 86400);  //关闭自动保存
define('WP_POST_REVISIONS', false);  //关闭修订
define('DISALLOW_FILE_EDIT', true);  //关闭文件修改
define('FS_CHMOD_DIR', (0755 & ~ umask()));  //目录权限755
define('FS_CHMOD_FILE', (0644 & ~ umask()));  //文件权限644

//禁止更新(不建议)
//define('AUTOMATIC_UPDATER_DISABLED', true);
//define('WP_AUTO_UPDATE_CORE', false);
  • 当前主题目录中function.php后添加下列内容
//WordPress主题 functions.php 优化项
//引入方式:在主题functions.php后添加

//移除不必要的信息,如WordPress版本
remove_action('wp_head', 'feed_links', 2);  //移除feed
remove_action('wp_head', 'feed_links_extra', 3);  //移除feed
remove_action('wp_head', 'rest_output_link_wp_head', 10); 
remove_action('wp_head', 'rsd_link');  //移除离线编辑器开放接口
remove_action('wp_head', 'wlwmanifest_link');  //移除离线编辑器开放接口
remove_action('wp_head', 'index_rel_link');  //去除本页唯一链接信息
remove_action('wp_head', 'parent_post_rel_link', 10, 0);  //清除前后文信息
remove_action('wp_head', 'start_post_rel_link', 10, 0);  //清除前后文信息
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
remove_action('wp_head', 'locale_stylesheet');
remove_action('publish_future_post','check_and_publish_future_post',10, 1);
remove_action('wp_head', 'noindex', 1);
remove_action('wp_head', 'wp_print_styles', 8);  //载入css
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_head', 'wp_generator');  //移除WordPress版本
remove_action('wp_head', 'rel_canonical');
remove_action('wp_footer', 'wp_print_footer_scripts');
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
remove_action('template_redirect', 'wp_shortlink_header', 11, 0);
add_action('widgets_init', 'my_remove_recent_comments_style');
function my_remove_recent_comments_style() {
    global $wp_widget_factory;
    remove_action('wp_head', array($wp_widget_factory->widgets['WP_Widget_Recent_Comments'] ,'recent_comments_style'));
}

//评论框禁止HTML
add_filter('pre_comment_content', 'wp_specialchars');

//停用链接猜测
add_filter('redirect_canonical', 'stop_guessing');
function stop_guessing($url) {
    if (is_404()) {
        return false;
    }
    return $url;
}

//禁止自动保存
remove_filter('the_content', 'wptexturize');
remove_action('pre_post_update', 'wp_save_post_revision');
add_action('wp_print_scripts', 'disable_autosave');
function disable_autosave() {
    wp_deregister_script('autosave');
}

//禁用wp-embed.min.js
function disable_embeds_init() {
    /* @var WP $wp */
    global $wp;

    // Remove the embed query var.
    $wp->public_query_vars = array_diff( $wp->public_query_vars, array(
        'embed',
    ) );

    // Remove the REST API endpoint.
    remove_action( 'rest_api_init', 'wp_oembed_register_route' );

    // Turn off
    add_filter( 'embed_oembed_discover', '__return_false' );

    // Don't filter oEmbed results.
    remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );

    // Remove oEmbed discovery links.
    remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );

    // Remove oEmbed-specific JavaScript from the front-end and back-end.
    remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' );

    // Remove all embeds rewrite rules.
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
}

add_action( 'init', 'disable_embeds_init', 9999 );

/**
 * Removes the 'wpembed' TinyMCE plugin.
 *
 * @since 1.0.0
 *
 * @param array $plugins List of TinyMCE plugins.
 * @return array The modified list.
 */
function disable_embeds_tiny_mce_plugin( $plugins ) {
    return array_diff( $plugins, array( 'wpembed' ) );
}

/**
 * Remove all rewrite rules related to embeds.
 *
 * @since 1.2.0
 *
 * @param array $rules WordPress rewrite rules.
 * @return array Rewrite rules without embeds rules.
 */
function disable_embeds_rewrites( $rules ) {
    foreach ( $rules as $rule => $rewrite ) {
        if ( false !== strpos( $rewrite, 'embed=true' ) ) {
            unset( $rules[ $rule ] );
        }
    }

    return $rules;
}

//Remove embeds rewrite rules on plugin activation.
function disable_embeds_remove_rewrite_rules() {
    add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' );
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' );

动静分离

将CSS/JS/图片等静态文件放在CDN,可以降低源站负载,提高加载速度。目前七牛、又拍云、腾讯云等均提供了免费额度,个人使用足矣。相关的教程均可以在搜索引擎上搜索到。

图片压缩

非壁纸类等需要原图分享的图片,尽量在 tinypng.com 之类的地方压缩后再上传,可以大幅提升加载速度,同时注意图片分辨率不能太大,高分辨率的图片自然也需要占用大量空间。

安全

未知来源的插件可能会被植入恶意代码,导致服务器被攻击,造成数据丢失等严重后果。建议直接从作者网站或Github下载插件及主题,避免使用来历不明的插件或主题。对于博客内容,务必按时做好备份。

环境配置

WordPress作为一款强大的程序,对性能的要求自然不低,服务器计算能力会直接影响到网站速度及并发。

PHP & MySQL

  • 选用高版本:PHP7及以上性能有极大提升,且PHP5也已经停止支持。
  • 启用PHP OPCache扩展
  • 合理优化MySQL参数
  • 对于较低配机器不建议使用MySQL 5.6及以上版本

使用 Memcached & Redis

网络

在选择主机或服务器时,如果可以,尽量使用国内的,速度能够得到保证。选择国外服务时,也需要注意线路,速度差的线路会对浏览体验产生极大影响。

评论

  1. 4年前
    2020-5-25 14:32:56

    美国主机套CF能起加速作用吗?

    • 博主
      Heresy
      4年前
      2020-5-26 23:42:54

      如果你的主机本来线路差,那么有用,如果是类似于CN2这样的高速线路,那么会起到反作用。

      • hiCasper
        4年前
        2020-5-26 23:46:24

        在用搬瓦工CN2,感觉用不用CF都差不多,不知道其他是不是也一样。

  2. bbslong
    4年前
    2020-3-19 14:01:21

    typecho主题只能加载两页,如何能无限加载呢?

  3. 4年前
    2019-12-08 22:13:58

    能帮我看下我的typecho for dux有啥问题吗,自己改了一些东西,感觉满了

  4. 5年前
    2019-7-04 17:08:01

    独立博客才用得到。

  5. 5年前
    2019-6-16 8:43:10

    你这个是tp吧

    • 博主
      心灵博客
      5年前
      2019-6-16 10:05:12

      是的,但我偶尔也会折腾下wp的2333

发送评论 编辑评论


				
上一篇
下一篇