2023/3/1026 次阅读0 条评论

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

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

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

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

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

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

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

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

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

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

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

Terminal
//※去掉烦人的文章历史版本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 ); //去除评论feedremove_action( 'wp_head', 'feed_links', 2 ); //去除文章feedremove_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 ); //移除后面文章的urlremove_action( 'wp_head', 'start_post_rel_link', 10, 0 ); //移除最开始文章的urlremove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );//自动生成的短链接remove_action( 'wp_head', 'adjacent_posts_rel_link', 10, 0 ); ///移除相邻文章的urlremove_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 sizeunset($sizes['large']); // disable large sizeunset($sizes['medium_large']); // disable medium-large sizeunset($sizes['1536x1536']); // disable 2x medium-large sizeunset($sizes['2048x2048']); // disable 2x large sizereturn $sizes;}

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

Terminal
/*彻底关闭自动更新(核心程序/主题/插件/翻译自动更新*/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;});

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插件安装后一键开启就不用管了。其他缓存插件,个人感觉意义不大,按个人所需选择吧。

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

Terminal
//显示页面查询次数、加载时间和内存占用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) ;?>

分享文章:
最后更新于: 2026/6/7
上一篇
个人使用的神级WordPress开发的Cursor规则Rules模板
下一篇
已经是最后的一篇文章了