欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言或关注公众号:烈日下的男人

WordPress_6.0.1_中文正式版发布及优化代码

网站源码 sky995 3年前 (2022-07-21) 964次浏览 0个评论

本文及资源最后更新时间 2022-07-21 by sky995

WordPress中文版是全球广泛使用的免费开源网站博客程序.WordPress最新版是一种采用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设自己的网站,也可以把WordPress当作博客到大型网站的内容管理系统(CMS).WordPress程序是免费的开源项目,在GNU通用公共许可证下授权发布.

WordPress_6.0.1_中文正式版发布及优化代码

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.  
  130. //移除后台页面title标题的wordpress后缀
  131. add_filter('admin_title', 'delAdminTitle', 10, 2);
  132. function delAdminTitle($admin_title, $title){
  133. return $title.' ‹ '.get_bloginfo('name');
  134. }
  135. //移除登陆页面title标题的wordpress后缀
  136. add_filter('login_title', 'remove_login_title', 10, 2);
  137. function remove_login_title($login_title, $title){
  138. return $title.' ‹ '.get_bloginfo('name');
  139. }
  140. /* 彻底禁止4.4+版之后响应式图片功能及缩略图裁剪功能*/
  141. // 禁止生成图像尺寸
  142. function zm_customize_image_sizes( $sizes ){
  143. unset( $sizes[ 'thumbnail' ]);
  144. unset( $sizes[ 'medium' ]);
  145. unset( $sizes[ 'medium_large' ] );
  146. unset( $sizes[ 'large' ]);
  147. unset( $sizes[ 'full' ] );
  148. unset( $sizes['1536x1536'] );
  149. unset( $sizes['2048x2048'] );
  150. return $sizes;
  151. }
  152. add_filter( 'intermediate_image_sizes_advanced', 'zm_customize_image_sizes' );
  153. // 禁止缩放图片尺寸
  154. add_filter('big_image_size_threshold', '__return_false');
  155. // 禁止生成其它图像尺寸
  156. function shapeSpace_disable_other_image_sizes() {
  157. // 禁止通过set_post_thumbnail_size()函数生成的图片尺寸
  158. remove_image_size('post-thumbnail');
  159. // 禁止添加其它图像尺寸
  160. remove_image_size('another-size');
  161. }
  162. add_action('init', 'shapeSpace_disable_other_image_sizes');
  163. //切换经典文章编辑器(v5.x开始默认古腾堡编辑器)
  164. add_filter('use_block_editor_for_post', '__return_false');
  165. //替换评论用户头像链接为国内镜像加速访问
  166. add_filter('get_avatar', function ($avatar) {
  167. return str_replace([
  168. 'www.gravatar.com/avatar/',
  169. '0.gravatar.com/avatar/',
  170. '1.gravatar.com/avatar/',
  171. '2.gravatar.com/avatar/',
  172. 'secure.gravatar.com/avatar/',
  173. 'cn.gravatar.com/avatar/'
  174. ], 'cravatar.cn/', $avatar);
  175. });
  176.  
  177. //取消内容转义
  178. remove_filter('the_content', 'wptexturize');
  179. //取消摘要转义
  180. remove_filter('the_excerpt', 'wptexturize');
  181. //取消评论转义
  182. remove_filter('comment_text', 'wptexturize');
  183. //禁止转义引号字符
  184. remove_filter('the_content', 'wptexturize'); // 禁止英文引号转义为中文引号
  185.  
  186. //文章插入图片自动移除 img 的 width、height、class 属性;
  187. add_filter( 'post_thumbnail_html', 'fanly_remove_images_attribute', 10 );
  188. add_filter( 'image_send_to_editor', 'fanly_remove_images_attribute', 10 );
  189. function fanly_remove_images_attribute( $html ) {
  190. //$html = preg_replace( '/(width|height)="\d*"\s/', "", $html );
  191. $html = preg_replace( '/width="(\d*)"\s+height="(\d*)"\s+class="[^"]*"/', "", $html );
  192. $html = preg_replace( '/ /', "", $html );
  193. return $html;
  194. }
  195.  
  196. //自适应图片删除width和height
  197. function ludou_remove_width_height_attribute($content){
  198. preg_match_all('/<[img|IMG].*?src=[\'|"](.*?(?:[\.gif|\.jpg|\.png\.webp]))[\'|"].*?[\/]?>/', $content, $images);
  199. if(!empty($images)) {
  200. foreach($images[0] as $index => $value){
  201. $new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]);
  202. $content = str_replace($images[0][$index], $new_img, $content);
  203. }
  204. }
  205. return $content;
  206. }
  207.  
  208. //判断是否是移动设备浏览
  209. if(wp_is_mobile()) {
  210. #删除文章内容中img的width和height属性
  211. add_filter('the_content', 'ludou_remove_width_height_attribute', 99);
  212. }
  213.  
  214. /* 删除文章时删除图片附件 */
  215. function delete_post_and_attachments($post_ID) {
  216. global $wpdb;
  217. #删除特色图片
  218. $thumbnails = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
  219. foreach ( $thumbnails as $thumbnail ) {
  220. wp_delete_attachment( $thumbnail->meta_value, true );
  221. }
  222. #删除图片附件
  223. $attachments = $wpdb->get_results( "SELECT * FROM $wpdb->posts WHERE post_parent = $post_ID AND post_type = 'attachment'" );
  224. foreach ( $attachments as $attachment ) {
  225. wp_delete_attachment( $attachment->ID, true );
  226. }
  227. $wpdb->query( "DELETE FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id' AND post_id = $post_ID" );
  228. }
  229. add_action('before_delete_post', 'delete_post_and_attachments');

新版变化

News & Announcements – WordPress.org
https://wordpress.org/news/

系统要求

WordPress 5.x 最低运行需求:
PHP5.6或更高版,建议7.2稳定版
MySQL5.6或更高版,建议5.6稳定版

下载地址

WordPress 6.0.1 简体中文正式版 (2022/07/12)

https://cn.wordpress.org/download/releases

https://cn.wordpress.org/wordpress-6.0.1-zh_CN.zip

https://cn.wordpress.org/wordpress-6.0.1-zh_CN.tar.gz


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:WordPress_6.0.1_中文正式版发布及优化代码
喜欢 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址