禁用 WordPress 相关无用功能

    1. <?php
    2. /*----------------------------------------------------------------------
    3. *
    4. * WordPress 性能优化
    5. *
    6. * !!!写入均是要将 WordPress 没用的东西关闭!!!
    7. *
    8. * @author 骚气靓丽的仔仔
    9. ------------------------------------------------------------------------*/
    10. foreach (array('rss2_head', 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head') as $action) {
    11. remove_action($action, 'the_generator');
    12. }
    13. //禁用 WordPress v5 古藤堡归为传统编辑器
    14. add_filter('use_block_editor_for_post', '__return_false');
    15. //屏蔽头部加载 s.w.org
    16. add_filter( 'emoji_svg_url', '__return_false' );
    17. //禁用一切头部的 <link rel='dns-prefetch' href='*****' />
    18. remove_action('wp_head', 'wp_resource_hints', 2);
    19. //删除 head 中的 RSD LINK <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/xmlrpc.php?rsd" />
    20. remove_action( 'wp_head', 'rsd_link' );
    21. //删除 head 中的 Windows Live Writer 的适配器 <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp-includes/wlwmanifest.xml" />
    22. remove_action( 'wp_head', 'wlwmanifest_link' );
    23. //删除 head 中的 WP 版本号 <meta name="generator" content="WordPress 3.5.1" />
    24. remove_action( 'wp_head', 'wp_generator');
    25. //删除 head 中的 Feed 相关的link
    26. remove_action( 'wp_head', 'feed_links_extra', 3 );
    27. //remove_action( 'wp_head', 'feed_links', 2 );
    28. //删除 head 中首页,上级,开始,相连的日志链接
    29. remove_action( 'wp_head', 'index_rel_link' );
    30. remove_action( 'wp_head', 'parent_post_rel_link', 10);
    31. remove_action( 'wp_head', 'start_post_rel_link', 10);
    32. remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10);
    33. ////删除 head 中的 shortlink
    34. remove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );
    35. // 删除头部输出 WP RSET API 地址 <link rel='https://api.w.org/' href='xxxx/wp-json/' />
    36. remove_action( 'wp_head', 'rest_output_link_wp_head', 10);
    37. //禁止短链接 Header 标签。
    38. remove_action( 'template_redirect', 'wp_shortlink_header', 11);
    39. // 禁止输出 Header Link 标签。
    40. remove_action( 'template_redirect', 'rest_output_link_header', 11);
    41. // 屏蔽 Emoji
    42. remove_action('admin_print_scripts','print_emoji_detection_script');
    43. remove_action('admin_print_styles', 'print_emoji_styles');
    44. remove_action('wp_head', 'print_emoji_detection_script', 7);
    45. remove_action('wp_print_styles', 'print_emoji_styles');
    46. remove_action('embed_head', 'print_emoji_detection_script');
    47. remove_filter('the_content_feed', 'wp_staticize_emoji');
    48. remove_filter('comment_text_rss', 'wp_staticize_emoji');
    49. remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
    50. add_filter('emoji_svg_url', '__return_false');
    51. add_filter('tiny_mce_plugins', function($plugins){
    52. return array_diff($plugins, ['wpemoji']);
    53. });
    54. //屏蔽字符转码 防止网站标题中的 “-” 被转义成 “& #8211;”
    55. add_filter('run_wptexturize', '__return_false');
    56. //移除 admin bar
    57. add_filter('show_admin_bar', '__return_false');
    58. //禁用 XML-RPC 接口
    59. add_filter( 'xmlrpc_enabled', '__return_false' );
    60. remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );
    61. //恢复链接管理器
    62. add_filter( 'pre_option_link_manager_enabled', '__return_true' );
    63. //禁止古腾堡加载 Google 字体
    64. add_action('admin_print_styles', function(){
    65. wp_deregister_style('wp-editor-font');
    66. wp_register_style('wp-editor-font', '');
    67. });
    68. //屏蔽文章 Embed 功能
    69. remove_action( 'rest_api_init', 'wp_oembed_register_route' );
    70. remove_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 );
    71. add_filter( 'embed_oembed_discover', '__return_false' );
    72. remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );
    73. remove_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );
    74. remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );
    75. remove_action( 'wp_head', 'wp_oembed_add_host_js' );
    76. add_filter('tiny_mce_plugins', function ($plugins){
    77. return array_diff( $plugins, ['wpembed'] );
    78. });
    79. //WordPress 5.0+移除 block-library CSS
    80. add_action( 'wp_enqueue_scripts', function(){
    81. wp_dequeue_style( 'wp-block-library' );
    82. });
    83. //彻底关闭 pingback
    84. add_filter('xmlrpc_methods',function($methods){
    85. $methods['pingback.ping'] = '__return_false';
    86. $methods['pingback.extensions.getPingbacks'] = '__return_false';
    87. return $methods;
    88. });
    89. //禁用 pingbacks, enclosures, trackbacks
    90. remove_action( 'do_pings', 'do_all_pings', 10 );
    91. //去掉 _encloseme 和 do_ping 操作。
    92. remove_action( 'publish_post','_publish_post_hook',5 );
    93. //彻底关闭 WordPress 核心、主题、插件自动更新功能
    94. add_filter('automatic_updater_disabled', '__return_true');// 彻底关闭自动更新
    95. remove_action('init', 'wp_schedule_update_checks');// 关闭更新检查定时作业
    96. wp_clear_scheduled_hook('wp_version_check');// 移除已有的版本检查定时作业
    97. wp_clear_scheduled_hook('wp_update_plugins');// 移除已有的插件更新定时作业
    98. wp_clear_scheduled_hook('wp_update_themes');// 移除已有的主题更新定时作业
    99. wp_clear_scheduled_hook('wp_maybe_auto_update');// 移除已有的自动更新定时作业
    100. remove_action( 'admin_init', '_maybe_update_core' );// 移除后台内核更新检查
    101. remove_action( 'load-plugins.php', 'wp_update_plugins' );// 移除后台插件更新检查
    102. remove_action( 'load-update.php', 'wp_update_plugins' );
    103. remove_action( 'load-update-core.php', 'wp_update_plugins' );
    104. remove_action( 'admin_init', '_maybe_update_plugins' );
    105. remove_action( 'load-themes.php', 'wp_update_themes' );// 移除后台主题更新检查
    106. remove_action( 'load-update.php', 'wp_update_themes' );
    107. remove_action( 'load-update-core.php', 'wp_update_themes' );
    108. remove_action( 'admin_init', '_maybe_update_themes' );
    109. add_filter('pre_site_transient_update_core', create_function('$a', "return null;")); // 关闭核心提示
    110. //删除WordPress网站静态资源URL地址的查询字符串
    111. function remove_query_strings_split($src){
    112. $output = preg_split("/(&ver|\?ver)/", $src);
    113. return $output[0];
    114. }
    115. add_action('init', function(){
    116. add_filter('script_loader_src', 'remove_query_strings_split', 15);
    117. add_filter('style_loader_src', 'remove_query_strings_split', 15);
    118. });
    119. //禁止文章修订
    120. define('WP_POST_REVISIONS', false );
    121. remove_action('pre_post_update', 'wp_save_post_revision');
    122. //禁用 WordPress 自动保存
    123. add_action('wp_print_scripts', function(){
    124. wp_deregister_script('autosave');
    125. });
    126. //禁用所有文章类型的修订版本
    127. add_filter( 'wp_revisions_to_keep', function($num, $post){
    128. return 0;
    129. },10,2);
    130. /**
    131. * 移除站点健康状态小工具
    132. * https://www.wpdaxue.com/wordpress-remove-site-health.html
    133. */
    134. function remove_dashboard_widget() {
    135. remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );
    136. }
    137. add_action('wp_dashboard_setup', 'remove_dashboard_widget' );
    138. /**
    139. * 移除 工具-站点健康 菜单
    140. * https://www.wpdaxue.com/wordpress-remove-site-health.html
    141. */
    142. function remove_site_health_menu(){
    143. remove_submenu_page( 'tools.php','site-health.php' );
    144. }
    145. add_action( 'admin_menu', 'remove_site_health_menu' );
    146. //禁用站点健康邮件通知
    147. add_filter( 'wp_fatal_error_handler_enabled', '__return_false' );
    148. //修改WordPress仪表盘中“查看站点”的打开方式为新标签页中打开 https://www.wpzhiku.com/change-view-site-link-open-target-to-new-table/
    149. function customize_my_wp_admin_bar( $wp_admin_bar ){
    150. //获取view-site 节点以便修改
    151. $node = $wp_admin_bar->get_node('view-site');
    152. //修改打开方式
    153. $node->meta['target'] = '_blank';
    154. //更新节点
    155. $wp_admin_bar->add_node($node);
    156. }
    157. add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 80 );
    158. //禁用WordPress 4.3中的可视化编辑器快捷方式 https://www.wpzhiku.com/disable-visual-editor-formatting-shortcuts-in-wordpress-4-3/
    159. function disable_mce_wptextpattern( $opt ) {
    160. if ( isset( $opt['plugins'] ) && $opt['plugins'] ) {
    161. $opt['plugins'] = explode( ',', $opt['plugins'] );
    162. $opt['plugins'] = array_diff( $opt['plugins'] , array( 'wptextpattern' ) );
    163. $opt['plugins'] = implode( ',', $opt['plugins'] );
    164. }
    165. return $opt;
    166. }
    167. add_filter( 'tiny_mce_before_init', 'disable_mce_wptextpattern' );