/', $content, $images); if (!empty($images) && is_array($images) && isset($images[0]) && is_array($images[0]) && !empty($images[0])) { foreach ($images[0] as $index => $value) { if (isset($images[0][$index]) && is_string($images[0][$index])) { $new_img = preg_replace('/(width|height)="\d*"\s/', "", $images[0][$index]); $content = str_replace($images[0][$index], $new_img, $content); } } } return $content; } // 判断是否是移动设备浏览 if (wp_is_mobile()) { add_filter('the_content', 'ludou_remove_width_height_attribute', 99); } /** * 彻底关闭自动更新(核心程序/主题/插件/翻译自动更新) */ 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 端口 */ function remove_xmlrpc_pingback_ping($methods) { // 确保 $methods 是数组 if (!is_array($methods)) { return $methods; } unset($methods['pingback.ping']); return $methods; } add_filter('xmlrpc_methods', 'remove_xmlrpc_pingback_ping'); /** * 移除前端网页源代码内的头部冗余代码 */ 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'); wp_dequeue_style('wp-block-library-theme'); wp_dequeue_style('wc-block-style'); // WooCommerce wp_dequeue_style('global-styles'); // WordPress 5.9+ } add_action('wp_enqueue_scripts', 'wpassist_remove_block_library_css', 100); /** * 移除前后台顶部工具栏指定菜单 */ function admin_bar_item(WP_Admin_Bar $admin_bar) { $admin_bar->remove_menu('wp-logo'); // 移除 wp 的 logo $admin_bar->remove_menu('updates'); // 移除更新提示 $admin_bar->remove_menu('comments'); // 移除评论提示 } add_action('admin_bar_menu', 'admin_bar_item', 500); /** * 移除后台仪表盘菜单:活动、新闻 */ function bzg_remove_dashboard_widgets() { global $wp_meta_boxes; // 确保 $wp_meta_boxes 是数组且包含必要的键 if (!is_array($wp_meta_boxes)) { return; } // 安全地移除 "活动" if (isset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity'])) { unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_activity']); } // 安全地移除 "WordPress 新闻" if (isset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary'])) { unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']); } } add_action('wp_dashboard_setup', 'bzg_remove_dashboard_widgets'); /** * 移除 login 页面的版权信息 */ function yqd_login_logo_url() { return home_url(); } add_filter('login_headerurl', 'yqd_login_logo_url'); /** * 自定义登录页面logo */ function custom_login_logo() { echo ''; } add_action('login_enqueue_scripts', 'custom_login_logo'); /** * 自定义登录页面logo标题 */ function custom_login_logo_title() { return get_bloginfo('name'); } add_filter('login_headertitle', 'custom_login_logo_title'); /** * 删除 WordPress 后台底部版权信息 */ function _admin_footer_right_text($text) { $text = ''; return $text; } add_filter('update_footer', '_admin_footer_right_text', 11); /** * 移除后台仪表盘菜单:帮助 */ function bzg_remove_help() { $screen = get_current_screen(); if ($screen && method_exists($screen, 'remove_help_tabs')) { $screen->remove_help_tabs(); } } add_action('admin_head', 'bzg_remove_help'); /** * 禁用 pingbacks, enclosures, trackbacks */ remove_action('do_pings', 'do_all_pings', 10); 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'); /** * 移除 DNS 预取 */ function remove_dns_prefetch($hints, $relation_type) { // 确保 $hints 是数组 if (!is_array($hints)) { return $hints; } if ('dns-prefetch' === $relation_type) { $unique_hosts = wp_dependencies_unique_hosts(); if (is_array($unique_hosts)) { return array_diff($hints, $unique_hosts); } } return $hints; } add_filter('wp_resource_hints', 'remove_dns_prefetch', 10, 2); /** * 替换评论用户头像链接为国内镜像加速源访问 */ add_filter('get_avatar', function ($avatar) { return str_replace([ 'www.gravatar.com/avatar/', '0.gravatar.com/avatar/', '1.gravatar.com/avatar/', '2.gravatar.com/avatar/', 'secure.gravatar.com/avatar/', 'cn.gravatar.com/avatar/' ], 'cravatar.cn/', $avatar); }); /** * 禁止查询网站静态资源连接版本字符 */ 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); /** * 支持 WebP 格式 */ function add_webp_mime_type($mimes) { $mimes['webp'] = 'image/webp'; return $mimes; } add_filter('mime_types', 'add_webp_mime_type'); /** * 支持 SVG 格式 */ function add_svg_mime_type($mimes) { $mimes['svg'] = 'image/svg+xml'; return $mimes; } add_filter('upload_mimes', 'add_svg_mime_type'); /** * 优化CSS加载 - 延迟加载非关键CSS */ function optimize_css_loading() { // 仅在前端执行 if (is_admin()) { return; } // 延迟加载非关键CSS add_filter('style_loader_tag', function($html, $handle, $href, $media) { // 定义非关键CSS样式表 $non_critical_styles = array( 'wp-block-library', 'wp-block-library-theme', 'wc-block-style', 'global-styles' ); if (in_array($handle, $non_critical_styles)) { // 使用preload + onload技术延迟加载 $html = str_replace("rel='stylesheet'", "rel='preload' as='style' onload=\"this.onload=null;this.rel='stylesheet'\"", $html); // 添加noscript fallback $html .= ''; } return $html; }, 10, 4); } add_action('wp_enqueue_scripts', 'optimize_css_loading', 1); /** * 优化JavaScript加载 */ function optimize_js_loading() { // 仅在前端执行 if (is_admin()) { return; } // 为非关键JS添加defer属性 add_filter('script_loader_tag', function($tag, $handle, $src) { // 定义需要defer的脚本 $defer_scripts = array( 'jquery-migrate', 'wp-embed', 'comment-reply' ); if (in_array($handle, $defer_scripts)) { return str_replace(' src', ' defer src', $tag); } return $tag; }, 10, 3); } add_action('wp_enqueue_scripts', 'optimize_js_loading', 1); /** * 移除不必要的CSS和JS */ function remove_unnecessary_assets() { // 仅在前端执行 if (is_admin()) { return; } // 移除WordPress默认样式 wp_dequeue_style('dashicons'); // 仅后台需要 // 移除不必要的JS wp_dequeue_script('wp-embed'); // 如果不使用评论功能,移除评论相关脚本 if (!is_single() && !is_page()) { wp_dequeue_script('comment-reply'); } } add_action('wp_enqueue_scripts', 'remove_unnecessary_assets', 100); /** * 添加关键CSS内联 */ function add_critical_css() { // 仅在前端执行 if (is_admin()) { return; } // 添加关键CSS到head中 echo ''; } add_action('wp_head', 'add_critical_css', 1); /** * 优化资源提示 */ function add_resource_hints() { // 仅在前端执行 if (is_admin()) { return; } // 添加DNS预连接 echo ''; echo ''; echo ''; } add_action('wp_head', 'add_resource_hints', 1); /** * 压缩HTML输出 */ function compress_html_output($buffer) { // 仅在前端压缩 if (is_admin()) { return $buffer; } // 移除HTML注释(保留IE条件注释) $buffer = preg_replace('/).)*-->/s', '', $buffer); // 移除多余的空白字符 $buffer = preg_replace('/\s+/', ' ', $buffer); // 移除行首行尾空白 $buffer = preg_replace('/^\s+|\s+$/m', '', $buffer); return $buffer; } // 启用HTML压缩(可选,可能影响某些插件) // add_action('template_redirect', function() { // if (!is_admin()) { // ob_start('compress_html_output'); // } // });