WordPress中文版作为一款全球广泛应用的免费开源博客与网站构建工具,基于PHP语言开发,用户能够在支持PHP及MySQL数据库的服务器环境中部署个人网站,或将其用作从简单博客到大型门户的内容管理系统(CMS)。作为开源项目,WordPress在GNU通用公共许可证的保护下免费发布,供全球用户自由使用和修改。
针对WordPress最新版本,推荐以下优化代码段:
<!--将以下优化代码添加至主题目录下的functions.php文件-->
/*完全禁用自动更新机制(涵盖核心程序、主题、插件及翻译更新)*/
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的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;
}
//禁用pingbacks、enclosures及trackbacks功能
remove_action( 'do_pings', 'do_all_pings', 10 );
//移除 _encloseme 和 do_ping 操作
remove_action( 'publish_post','_publish_post_hook',5 );
/* 禁止加载s.w.org以获取表情和头像资源 */
remove_action('wp_head', 'print_emoji_detection_script', 7 );
remove_action('admin_print_scripts','print_emoji_detection_script');
remove_action('wp_print_styles', 'print_emoji_styles');
remove_action('admin_print_styles', 'print_emoji_styles');
function remove_dns_prefetch( $hints, $relation_type ) {
if ( 'dns-prefetch' === $relation_type ) {
return array_diff( wp_dependencies_unique_hosts(), $hints );
}
return $hints;
}
add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
/* 彻底禁用REST API并移除wp-json链接 */
function lerm_disable_rest_api( $access ) {
return new WP_Error(
'Stop!',
'Soooooryyyy',
array(
'status' => 403,
)
);
}
add_filter( 'rest_authentication_errors', 'lerm_disable_rest_api' );
remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
/* 禁止查询网站静态资源的版本字符 */
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 );
/* 移除前端网页源代码中的头部冗余代码 */
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 wpassist_remove_block_library_css(){
wp_dequeue_style( 'wp-block-library' );
}
remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
add_action( 'wp_enqueue_scripts', 'wpassist_remove_block_library_css' );
/* 移除新版本站点健康状态面板及菜单项 */
add_action( 'admin_menu', 'remove_site_health_menu' );
function remove_site_health_menu(){
remove_submenu_page( 'tools.php','site-health.php' );
}
/* 禁用5.5版本后自带的XML站点地图功能 */
add_filter( 'wp_sitemaps_enabled', '__return_false' );
/* 移除前后台顶部工具栏中的指定菜单项 */
function admin_bar_item ( WP_Admin_Bar $admin_bar ) {
$admin_bar->remove_menu('wp-logo'); //移除WordPress的logo
$admin_bar->remove_menu('site-name'); //移除站点名称
$admin_bar->remove_menu('updates'); //移除更新提示
$admin_bar->remove_menu('comments'); //移除评论提示
/*$admin_bar->remove_menu('new-content'); //移除新建按钮 */
}
add_action( 'adm
安卿辰博客








