我爱辅助吧

 找回密码
 立即注册
查看: 276|回复: 1

[Windows] WordPress中文版(网站博客程序) v6.9 正式版-优化教程

[复制链接]
  • TA的每日心情

    11 小时前
  • 签到天数: 1173 天

    [LV.10]以坛为家III

    1万

    主题

    1万

    帖子

    56万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    565927
    发表于 2025-12-3 10:15:00 | 显示全部楼层 |阅读模式
    WordPress中文版是全球广泛使用的免费开源网站博客程序.WordPress最新版是一种采用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设自己的网站,也可以把WordPress当作博客到大型网站的内容管理系统(CMS).WordPress程序是免费的开源项目,在GNU通用公共许可证下授权发布.
    WordPress新版必备优化代码:
    1. <!--添加优化代码到主题目录functions.php文件-->
    2. /*彻底关闭自动更新(核心程序/主题/插件/翻译自动更新*/
    3. add_filter('automatic_updater_disabled', '__return_true');
    4. /*关闭更新检查定时作业*/
    5. remove_action('init', 'wp_schedule_update_checks');
    6. /*移除已有的版本检查定时作业*/
    7. wp_clear_scheduled_hook('wp_version_check');
    8. /*移除已有的插件更新定时作业*/
    9. wp_clear_scheduled_hook('wp_update_plugins');
    10. /*移除已有的主题更新定时作业*/
    11. wp_clear_scheduled_hook('wp_update_themes');
    12. /*移除已有的自动更新定时作业*/
    13. wp_clear_scheduled_hook('wp_maybe_auto_update');
    14. /*移除后台内核更新检查*/
    15. remove_action( 'admin_init', '_maybe_update_core' );
    16. /*移除后台插件更新检查*/
    17. remove_action( 'load-plugins.php', 'wp_update_plugins' );
    18. remove_action( 'load-update.php', 'wp_update_plugins' );
    19. remove_action( 'load-update-core.php', 'wp_update_plugins' );
    20. remove_action( 'admin_init', '_maybe_update_plugins' );
    21. /*移除后台主题更新检查*/
    22. remove_action( 'load-themes.php', 'wp_update_themes' );
    23. remove_action( 'load-update.php', 'wp_update_themes' );
    24. remove_action( 'load-update-core.php', 'wp_update_themes' );
    25. remove_action( 'admin_init', '_maybe_update_themes' );
    26. /*关闭程序更新提示*/
    27. add_filter( 'pre_site_transient_update_core', function($a){ return null; });
    28. /*关闭插件更新提示*/
    29. add_filter('pre_site_transient_update_plugins', function($a){return null;});
    30. /*关闭主题更新提示*/
    31. add_filter('pre_site_transient_update_themes', function($a){return null;});
    32. //关闭WordPress的XML-RPC功能
    33. add_filter('xmlrpc_enabled', '__return_false');
    34. /* 关闭XML-RPC的pingback端口 */
    35. add_filter( 'xmlrpc_methods', 'remove_xmlrpc_pingback_ping' );
    36. function remove_xmlrpc_pingback_ping( $methods ) {
    37. unset( $methods['pingback.ping'] );
    38. return $methods;
    39. }
    40. //禁用 pingbacks, enclosures, trackbacks
    41. remove_action( 'do_pings', 'do_all_pings', 10 );
    42. //去掉 _encloseme 和 do_ping 操作
    43. remove_action( 'publish_post','_publish_post_hook',5 );
    44. /* 禁止加载s.w.org获取表情和头像 */
    45. remove_action('wp_head', 'print_emoji_detection_script', 7 );
    46. remove_action('admin_print_scripts','print_emoji_detection_script');
    47. remove_action('wp_print_styles', 'print_emoji_styles');
    48. remove_action('admin_print_styles', 'print_emoji_styles');
    49. function remove_dns_prefetch( $hints, $relation_type ) {
    50. if ( 'dns-prefetch' === $relation_type ) {
    51. return array_diff( wp_dependencies_unique_hosts(), $hints );
    52. }
    53. return $hints;
    54. }
    55. add_filter( 'wp_resource_hints', 'remove_dns_prefetch', 10, 2 );
    56. /* 完全禁止REST API、移除wp-json链接 */
    57. function lerm_disable_rest_api( $access ) {
    58.         return new WP_Error(
    59.                 'Stop!',
    60.                 'Soooooryyyy',
    61.                 array(
    62.                         'status' => 403,
    63.                 )
    64.         );
    65. }
    66. add_filter( 'rest_authentication_errors', 'lerm_disable_rest_api' );
    67. remove_action( 'wp_head', 'rest_output_link_wp_head', 10 );
    68. /* 禁止查询网站静态资源连接版本字符 */
    69. function _remove_script_version ( $src ){
    70.   $parts = explode( '?', $src );
    71.   return $parts[0];
    72. }
    73. add_filter( 'script_loader_src', '_remove_script_version', 15, 1 );
    74. add_filter( 'style_loader_src', '_remove_script_version', 15, 1 );
    75. /* 移除前端网页源代码内的头部冗余代码 */
    76. remove_action( 'wp_head', 'feed_links_extra', 3 );
    77. remove_action( 'wp_head', 'rsd_link' );
    78. remove_action( 'wp_head', 'wlwmanifest_link' );
    79. remove_action( 'wp_head', 'index_rel_link' );
    80. remove_action( 'wp_head', 'start_post_rel_link', 10, 0 );
    81. remove_action( 'wp_head', 'wp_generator' );
    82. /* 禁止新版文章编辑器加载前端样式 */
    83. function wpassist_remove_block_library_css(){
    84.     wp_dequeue_style( 'wp-block-library' );
    85. }
    86. remove_action( 'wp_enqueue_scripts', 'wp_common_block_scripts_and_styles' );
    87. add_action( 'wp_enqueue_scripts', 'wpassist_remove_block_library_css' );
    88. /* 移除新版本站点健康状态面板和菜单项 */
    89. add_action( 'admin_menu', 'remove_site_health_menu' );
    90. function remove_site_health_menu(){
    91. remove_submenu_page( 'tools.php','site-health.php' );
    92. }
    93. /* 禁用5.5版后自带的XML站点地图 */
    94. add_filter( 'wp_sitemaps_enabled', '__return_false' );
    95. /* 移除前后台顶部工具栏指定菜单 */
    96. function admin_bar_item ( WP_Admin_Bar $admin_bar ) {
    97.         $admin_bar->remove_menu('wp-logo'); //移动wp的logo
    98.         $admin_bar->remove_menu('site-name'); //移动站点名称
    99.         $admin_bar->remove_menu('updates'); //移动更新提示
    100.         $admin_bar->remove_menu('comments'); //移动评论提示
    101.         /*$admin_bar->remove_menu('new-content');        //移除新建按钮  */
    102. }
    103. add_action( 'admin_bar_menu', 'admin_bar_item', 500 );
    104. //移除后台仪表盘站点健康状态面板
    105. add_action('wp_dashboard_setup', 'remove_site_health_dashboard_widget');
    106. function remove_site_health_dashboard_widget()
    107. {
    108.     remove_meta_box('dashboard_site_health', 'dashboard', 'normal');
    109. }
    110. //移除后台仪表盘菜单:站点健康状态
    111. add_action( 'admin_menu', 'remove_site_health_menu' );       
    112. function remove_site_health_menu(){
    113.         remove_submenu_page( 'tools.php','site-health.php' );
    114. }
    115. //移除后台仪表盘菜单:活动、新闻
    116. function bzg_remove_dashboard_widgets() {
    117.         global $wp_meta_boxes;
    118.         #移除 "活动"
    119.         unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']);
    120.         #移除 "WordPress 新闻"
    121.         unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
    122. }
    123. add_action('wp_dashboard_setup', 'bzg_remove_dashboard_widgets' );
    124. //移除后台仪表盘菜单:帮助
    125. function bzg_remove_help() {
    126.         get_current_screen()->remove_help_tabs();
    127. }
    128. add_action('admin_head', 'bzg_remove_help');

    129. //移除后台页面title标题的wordpress后缀
    130. add_filter('admin_title', 'delAdminTitle', 10, 2);
    131. function delAdminTitle($admin_title, $title){
    132.     return $title.' ‹ '.get_bloginfo('name');
    133. }
    134. //移除登陆页面title标题的wordpress后缀
    135. add_filter('login_title', 'remove_login_title', 10, 2);
    136. function remove_login_title($login_title, $title){
    137.         return $title.' ‹ '.get_bloginfo('name');
    138. }
    139. /* 彻底禁止4.4+版之后响应式图片功能及缩略图裁剪功能*/
    140. // 禁止生成图像尺寸
    141. function zm_customize_image_sizes( $sizes ){
    142.         unset( $sizes[ 'thumbnail' ]);
    143.         unset( $sizes[ 'medium' ]);
    144.         unset( $sizes[ 'medium_large' ] );
    145.         unset( $sizes[ 'large' ]);
    146.         unset( $sizes[ 'full' ] );
    147.         unset( $sizes['1536x1536'] );
    148.         unset( $sizes['2048x2048'] );
    149.         return $sizes;
    150. }

    151. add_filter( 'intermediate_image_sizes_advanced', 'zm_customize_image_sizes' );

    152. // 禁止缩放图片尺寸
    153. add_filter('big_image_size_threshold', '__return_false');

    154. // 禁止生成其它图像尺寸
    155. function shapeSpace_disable_other_image_sizes() {       
    156.         // 禁止通过set_post_thumbnail_size()函数生成的图片尺寸
    157.         remove_image_size('post-thumbnail');
    158.         // 禁止添加其它图像尺寸
    159.         remove_image_size('another-size');
    160. }
    161. add_action('init', 'shapeSpace_disable_other_image_sizes');
    162. //切换经典文章编辑器(v5.x开始默认古腾堡编辑器)
    163. add_filter('use_block_editor_for_post', '__return_false');
    164. //替换评论用户头像链接为国内镜像加速访问
    165. add_filter('get_avatar', function ($avatar) {
    166. return str_replace([
    167. 'www.gravatar.com/avatar/',
    168. '0.gravatar.com/avatar/',
    169. '1.gravatar.com/avatar/',
    170. '2.gravatar.com/avatar/',
    171. 'secure.gravatar.com/avatar/',
    172. 'cn.gravatar.com/avatar/'
    173. ], 'cravatar.cn/', $avatar);
    174. });

    175. //取消内容转义
    176. remove_filter('the_content', 'wptexturize');
    177. //取消摘要转义
    178. remove_filter('the_excerpt', 'wptexturize');
    179. //取消评论转义
    180. remove_filter('comment_text', 'wptexturize');
    181. //禁止转义引号字符
    182. remove_filter('the_content', 'wptexturize');    // 禁止英文引号转义为中文引号

    183. //文章插入图片自动移除 img 的 width、height、class 属性;
    184. add_filter( 'post_thumbnail_html', 'fanly_remove_images_attribute', 10 );
    185. add_filter( 'image_send_to_editor', 'fanly_remove_images_attribute', 10 );
    186. function fanly_remove_images_attribute( $html ) {
    187. //$html = preg_replace( '/(width|height)="d*"s/', "", $html );
    188. $html = preg_replace( '/width="(d*)"s+height="(d*)"s+class="[^"]*"/', "", $html );
    189. $html = preg_replace( '/  /', "", $html );
    190. return $html;
    191. }

    192. //自适应图片删除width和height
    193. function ludou_remove_width_height_attribute($content){
    194.   preg_match_all('/<[img|IMG].*?src=['|"](.*?(?:[.gif|.jpg|.png.webp]))['|"].*?[/]?>/', $content, $images);
    195.   if(!empty($images)) {
    196.     foreach($images[0] as $index => $value){
    197.       $new_img = preg_replace('/(width|height)="d*"s/', "", $images[0][$index]);
    198.       $content = str_replace($images[0][$index], $new_img, $content);
    199.     }
    200.   }
    201.   return $content;
    202. }

    203. //判断是否是移动设备浏览
    204. if(wp_is_mobile()) {
    205.    #删除文章内容中img的width和height属性
    206.    add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
    207. }

    208. /* 删除文章时删除图片附件 */
    209. function delete_post_and_attachments($post_ID) {
    210. global $wpdb;
    211. #删除特色图片
    212. $thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
    213. foreach ( $thumbnails as $thumbnail ) {
    214. wp_delete_attachment( $thumbnail->meta_value, true );
    215. }
    216. #删除图片附件
    217. $attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );
    218. foreach ( $attachments as $attachment ) {
    219. wp_delete_attachment( $attachment->ID, true );
    220. }
    221. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
    222. }
    223. add_action('before_delete_post', 'delete_post_and_attachments');
    复制代码
    系统要求
    WordPress 5.x以上版本最低运行需求:
    PHP5.6或更高版,建议PHP7.2稳定版
    MySQL5.6或更高版,建议用5.6稳定版
    游客,如果您要查看本帖隐藏内容请回复
    我爱辅助吧论坛提示

    免责声明:

    本站提供的资源,都来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,我们不保证内容的长久可用性,通过使用本站内容随之而来的风险与本站无关,您必须在下载后的24个小时之内,从您的电脑/手机中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信E-mail:viww6359@789cdk.com 飞机TG:@milianfc


    回复

    使用道具 举报

  • TA的每日心情

    6 天前
  • 签到天数: 97 天

    [LV.6]常住居民II

    8

    主题

    202

    帖子

    1171

    积分

    金牌会员

    Rank: 6Rank: 6

    积分
    1171
    发表于 2025-12-12 01:06:17 | 显示全部楼层
    顶顶顶顶顶顶顶顶顶
    回复

    使用道具 举报

    懒得打字嘛,点击右侧快捷回复 【网址发布页 - www.6fb.top】
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    Archiver|小黑屋|我爱辅助吧

    GMT+8, 2025-12-22 20:17 , Processed in 0.045843 second(s), 26 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

    快速回复 返回顶部 返回列表