禁用 WordPress 相关无用功能
<?php/*----------------------------------------------------------------------** WordPress 性能优化** !!!写入均是要将 WordPress 没用的东西关闭!!!** @author 骚气靓丽的仔仔------------------------------------------------------------------------*/foreach (array('rss2_head', 'commentsrss2_head', 'rss_head', 'rdf_header', 'atom_head', 'comments_atom_head', 'opml_head', 'app_head') as $action) {remove_action($action, 'the_generator');}//禁用 WordPress v5 古藤堡归为传统编辑器add_filter('use_block_editor_for_post', '__return_false');//屏蔽头部加载 s.w.orgadd_filter( 'emoji_svg_url', '__return_false' );//禁用一切头部的 <link rel='dns-prefetch' href='*****' />remove_action('wp_head', 'wp_resource_hints', 2);//删除 head 中的 RSD LINK <link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://example.com/xmlrpc.php?rsd" />remove_action( 'wp_head', 'rsd_link' );//删除 head 中的 Windows Live Writer 的适配器 <link rel="wlwmanifest" type="application/wlwmanifest+xml" href="http://example.com/wp-includes/wlwmanifest.xml" />remove_action( 'wp_head', 'wlwmanifest_link' );//删除 head 中的 WP 版本号 <meta name="generator" content="WordPress 3.5.1" />remove_action( 'wp_head', 'wp_generator');//删除 head 中的 Feed 相关的linkremove_action( 'wp_head', 'feed_links_extra', 3 );//remove_action( 'wp_head', 'feed_links', 2 );//删除 head 中首页,上级,开始,相连的日志链接remove_action( 'wp_head', 'index_rel_link' );remove_action( 'wp_head', 'parent_post_rel_link', 10);remove_action( 'wp_head', 'start_post_rel_link', 10);remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10);////删除 head 中的 shortlinkremove_action( 'wp_head', 'wp_shortlink_wp_head', 10, 0 );// 删除头部输出 WP RSET API 地址 <link rel='https://api.w.org/' href='xxxx/wp-json/' />remove_action( 'wp_head', 'rest_output_link_wp_head', 10);//禁止短链接 Header 标签。remove_action( 'template_redirect', 'wp_shortlink_header', 11);// 禁止输出 Header Link 标签。remove_action( 'template_redirect', 'rest_output_link_header', 11);// 屏蔽 Emojiremove_action('admin_print_scripts','print_emoji_detection_script');remove_action('admin_print_styles', 'print_emoji_styles');remove_action('wp_head', 'print_emoji_detection_script', 7);remove_action('wp_print_styles', 'print_emoji_styles');remove_action('embed_head', 'print_emoji_detection_script');remove_filter('the_content_feed', 'wp_staticize_emoji');remove_filter('comment_text_rss', 'wp_staticize_emoji');remove_filter('wp_mail', 'wp_staticize_emoji_for_email');add_filter('emoji_svg_url', '__return_false');add_filter('tiny_mce_plugins', function($plugins){return array_diff($plugins, ['wpemoji']);});//屏蔽字符转码 防止网站标题中的 “-” 被转义成 “& #8211;”add_filter('run_wptexturize', '__return_false');//移除 admin baradd_filter('show_admin_bar', '__return_false');//禁用 XML-RPC 接口add_filter( 'xmlrpc_enabled', '__return_false' );remove_action( 'xmlrpc_rsd_apis', 'rest_output_rsd' );//恢复链接管理器add_filter( 'pre_option_link_manager_enabled', '__return_true' );//禁止古腾堡加载 Google 字体add_action('admin_print_styles', function(){wp_deregister_style('wp-editor-font');wp_register_style('wp-editor-font', '');});//屏蔽文章 Embed 功能remove_action( 'rest_api_init', 'wp_oembed_register_route' );remove_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10, 4 );add_filter( 'embed_oembed_discover', '__return_false' );remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 );remove_filter( 'oembed_response_data', 'get_oembed_response_data_rich', 10, 4 );remove_action( 'wp_head', 'wp_oembed_add_discovery_links' );remove_action( 'wp_head', 'wp_oembed_add_host_js' );add_filter('tiny_mce_plugins', function ($plugins){return array_diff( $plugins, ['wpembed'] );});//WordPress 5.0+移除 block-library CSSadd_action( 'wp_enqueue_scripts', function(){wp_dequeue_style( 'wp-block-library' );});//彻底关闭 pingbackadd_filter('xmlrpc_methods',function($methods){$methods['pingback.ping'] = '__return_false';$methods['pingback.extensions.getPingbacks'] = '__return_false';return $methods;});//禁用 pingbacks, enclosures, trackbacksremove_action( 'do_pings', 'do_all_pings', 10 );//去掉 _encloseme 和 do_ping 操作。remove_action( 'publish_post','_publish_post_hook',5 );//彻底关闭 WordPress 核心、主题、插件自动更新功能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', create_function('$a', "return null;")); // 关闭核心提示//删除WordPress网站静态资源URL地址的查询字符串function remove_query_strings_split($src){$output = preg_split("/(&ver|\?ver)/", $src);return $output[0];}add_action('init', function(){add_filter('script_loader_src', 'remove_query_strings_split', 15);add_filter('style_loader_src', 'remove_query_strings_split', 15);});//禁止文章修订define('WP_POST_REVISIONS', false );remove_action('pre_post_update', 'wp_save_post_revision');//禁用 WordPress 自动保存add_action('wp_print_scripts', function(){wp_deregister_script('autosave');});//禁用所有文章类型的修订版本add_filter( 'wp_revisions_to_keep', function($num, $post){return 0;},10,2);/*** 移除站点健康状态小工具* https://www.wpdaxue.com/wordpress-remove-site-health.html*/function remove_dashboard_widget() {remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );}add_action('wp_dashboard_setup', 'remove_dashboard_widget' );/*** 移除 工具-站点健康 菜单* https://www.wpdaxue.com/wordpress-remove-site-health.html*/function remove_site_health_menu(){remove_submenu_page( 'tools.php','site-health.php' );}add_action( 'admin_menu', 'remove_site_health_menu' );//禁用站点健康邮件通知add_filter( 'wp_fatal_error_handler_enabled', '__return_false' );//修改WordPress仪表盘中“查看站点”的打开方式为新标签页中打开 https://www.wpzhiku.com/change-view-site-link-open-target-to-new-table/function customize_my_wp_admin_bar( $wp_admin_bar ){//获取view-site 节点以便修改$node = $wp_admin_bar->get_node('view-site');//修改打开方式$node->meta['target'] = '_blank';//更新节点$wp_admin_bar->add_node($node);}add_action( 'admin_bar_menu', 'customize_my_wp_admin_bar', 80 );//禁用WordPress 4.3中的可视化编辑器快捷方式 https://www.wpzhiku.com/disable-visual-editor-formatting-shortcuts-in-wordpress-4-3/function disable_mce_wptextpattern( $opt ) {if ( isset( $opt['plugins'] ) && $opt['plugins'] ) {$opt['plugins'] = explode( ',', $opt['plugins'] );$opt['plugins'] = array_diff( $opt['plugins'] , array( 'wptextpattern' ) );$opt['plugins'] = implode( ',', $opt['plugins'] );}return $opt;}add_filter( 'tiny_mce_before_init', 'disable_mce_wptextpattern' );
