安装好WordPress后必要的优化工作 (2023版)

26次阅读
没有评论

自用整理,总所周知 Wordpress 现在是越来越臃肿了,虽然服务器、PHP 性能都有飞升,但如果你需要尽可能使用小成本 VPS 应对大量访问,

特别 CN 使用者就必须做好各种优化。有些插件也提供相似的内容,这里整理的是自用函数代码。

针对 wp-config.php 文件的优化

首先开启强制后台 HTTPS 访问,以防止后台重定向过多打不开的问题:

$_SERVER['HTTPS'] = 'on';
define('FORCE_SSL_LOGIN', true);
define('FORCE_SSL_ADMIN', true);

减少自动保存次数:
define('WP_POST_REVISIONS', 3);
或是禁止自动保存:
define('WP_POST_REVISIONS', false);
启用内置数据库优化:
任何人都可以使用数据库优化界面(即使是未登录的访问者)。仅在您要运行优化工具的时间段内启用该功能,然后禁用它。

# 启用 WordPress 数据库优化功能
define('WP_ALLOW_REPAIR', true);

重定向不存在的子域名和子文件夹到主页:
define( 'NOBLOGREDIRECT', 'http://www.yourwebsite.com' );

针对主题的 functions.php 文件的优化

打※号的优化个人认为有比较大的意义

//※去掉烦人的文章历史版本
define('WP_POST_REVISIONS', false);
// 防止 XML-RPC 攻击,浪费大量服务器资源
add_filter('xmlrpc_enabled', '__return_false');
// 关闭 XML-RPC 的 pingback 端口
add_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping');
function remove_xmlrpc_pingback_ping($methods) {unset( $methods['pingback.ping'] );
return $methods;
// 代码干净,强迫症专用
remove_action('wp_head', 'feed_links_extra', 3); // 去除评论 feed
remove_action('wp_head', 'feed_links', 2); // 去除文章 feed
remove_action('wp_head', 'rsd_link'); // 针对 Blog 的远程离线编辑器接口
remove_action('wp_head', 'wlwmanifest_link'); //Windows Live Writer 接口
remove_action('wp_head', 'index_rel_link'); // 移除当前页面的索引
remove_action('wp_head', 'parent_post_rel_link', 10, 0); // 移除后面文章的 url
remove_action('wp_head', 'start_post_rel_link', 10, 0); // 移除最开始文章的 url
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);// 自动生成的短链接
remove_action('wp_head', 'adjacent_posts_rel_link', 10, 0); /// 移除相邻文章的 url
remove_action('wp_head', 'wp_generator'); // 移除版本号,可能会增加安全性
}
//※※※切换经典文章编辑器(不使用古腾堡编辑器)add_filter('use_block_editor_for_post', '__return_false');
// 移除前端网页源代码内的头部冗余代码
remove_action('wp_head', 'feed_links_extra', 3); 
remove_action('wp_head', 'rsd_link'); 
remove_action('wp_head', 'wlwmanifest_link'); 
remove_action('wp_head', 'index_rel_link'); 
remove_action('wp_head', 'start_post_rel_link', 10, 0); 
remove_action('wp_head', 'wp_generator'); 
//※※※上传文件自动更名(此函数不改标题)function wp_coderbusy_sanitize_file_name($filename) {$time = date("dHis");
    return $time . "". mt_rand(100, 999) ."." . pathinfo($filename, PATHINFO_EXTENSION);
}
add_filter('sanitize_file_name', 'wp_coderbusy_sanitize_file_name', 10, 1);

//※※※禁用不需要的自动生成的图片尺寸
function shapeSpace_disable_image_sizes($sizes) {unset($sizes['medium']); // disable medium size
unset($sizes['large']); // disable large size
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size
return $sizes;
}

//※※※搜索只匹配标题
add_filter('posts_search', 'ytkah_search_by_title', 10, 2);
function ytkah_search_by_title($search, $wp_query) {if ( ! empty( $search) && ! empty($wp_query->query_vars['search_terms'] ) ) {
        global $wpdb;
        $q = $wp_query->query_vars;
        $n = ! empty($q['exact'] ) ? '':'%';
        $search = array();
        foreach (( array) $q['search_terms'] as $term )
            $search[] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $n . $wpdb->esc_like( $term) . $n );
        if (! is_user_logged_in() )
            $search[] = "$wpdb->posts.post_password =''";
        $search = 'AND' . implode('AND', $search);
    }
    return $search;
}

根据自己需要自选下面的函数:

/* 彻底关闭自动更新(核心程序 / 主题 / 插件 / 翻译自动更新 */
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');
/* 关闭程序更新提示 */
add_filter('pre_site_transient_update_core', function($a){return null;});
/* 关闭插件更新提示 */
add_filter('pre_site_transient_update_plugins', function($a){return null;});
/* 关闭主题更新提示 */
add_filter('pre_site_transient_update_themes', function($a){return null;});

// 移除新版本站点健康状态面板和菜单项
add_action('admin_menu', 'remove_site_health_menu'); 
function remove_site_health_menu(){remove_submenu_page( 'tools.php','site-health.php'); 
}
// 禁用自带的 XML 站点地图
add_filter('wp_sitemaps_enabled', '__return_false');
// 移除后台仪表盘站点健康状态面板
add_action('wp_dashboard_setup', 'remove_site_health_dashboard_widget');
function remove_site_health_dashboard_widget()
{remove_meta_box('dashboard_site_health', 'dashboard', 'normal');
}
// 移除后台仪表盘菜单:站点健康状态
add_action('admin_menu', 'remove_site_health_menu');    
function remove_site_health_menu(){remove_submenu_page( 'tools.php','site-health.php'); 
}
// 移除后台仪表盘菜单:活动、新闻
function bzg_remove_dashboard_widgets() {
  global $wp_meta_boxes;
  #移除 "活动" 
  unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
  #移除 "WordPress 新闻" 
  unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
}
add_action('wp_dashboard_setup', 'bzg_remove_dashboard_widgets');
// 移除后台仪表盘菜单:帮助
function bzg_remove_help() {get_current_screen()->remove_help_tabs();}
add_action('admin_head', 'bzg_remove_help');
// 移除后台页面 title 标题的 wordpress 后缀
add_filter('admin_title', 'delAdminTitle', 10, 2);
function delAdminTitle($admin_title, $title){return $title.'‹'.get_bloginfo('name');
}
// 移除登陆页面 title 标题的 wordpress 后缀
add_filter('login_title', 'remove_login_title', 10, 2);
function remove_login_title($login_title, $title){return $title.'‹'.get_bloginfo('name');
}
// 禁止 WordPress 新版本文章编辑器前端加载样式文件
remove_action('wp_enqueue_scripts', 'wp_common_block_scripts_and_styles');
// 替换评论用户头像链接为国内镜像加速访问
add_filter('get_avatar', function ($avatar) {
return str_replace([
'www.gravatar.com/avatar/',
'0.gravatar.com/avatar/',
'1.gravatar.com/avatar/',
'2.gravatar.com/avatar/',
'secure.gravatar.com/avatar/',
'cn.gravatar.com/avatar/'
], 'gravatar.wp-china-yes.net/', $avatar);
});

WordPress 性能优化

php 在执行函数最消耗时间的就是查询 SQL 数据库了,一般来讲一个页面的数据库查询大概在 120 到 260 次左右,使用 Redis 或者 Memcached 缓存,原理就是将 php 查询过的数据库缓存下来,下一次相同内容就不再查询数据库了,直接从缓存获取,极大的提高 php 执行效率。

WordPress 官方采用 Object Cache 对象缓存,同时主题的也遵循此机制,配合 Redis 或者 Memcached 缓存,能将数据库查询降低 80%,极大的提高了渲染速度。

经过本人的数年实战经验,Memcached 的 BUG 有点太多了,不适合小白,实测还是建议服务器开启 Opcache+Redis 性能和稳定最好,但注意内存小于 1G 的可能要开启虚拟 swap 才能安装 Redis,安装完成后再搜索 Redis Object Cache 插件 安装后一键开启就不用管了。其他缓存插件,个人感觉意义不大,按个人所需选择吧。

可以用以下代码测试性能改善的效果:

// 显示页面查询次数、加载时间和内存占用
function boyy_performance($visible = false) {$stat = sprintf( '[%d 次查询 / 耗时 %.3f seconds/ %.2fMB 内存]',
 get_num_queries(),
 timer_stop(0, 3),
 memory_get_peak_usage() / 1024 / 1024);
 echo $visible ? $stat : "<!-- {$stat} -->" ;
}

Performance 的参数 ture 表示在页面前端显示。如果你想在页面中不显示,只在 html 源码中可见可改为 false。
<?php if(function_exists('boyy_performance')) boyy_performance(true) ;?>

正文完
 0
评论(没有评论)
验证码