1. Đoạn mã này làm những gì?
- Đơn giản hóa trang admin: ẩn bớt menu, tắt file editor, bỏ news widget, dọn admin bar, ẩn help tab.
- Tối ưu frontend cho Classic Theme: tắt Gutenberg, block CSS, Global Styles, classic-theme-styles, emoji, embed, RSS, jQuery (nếu bạn không dùng).
- Ưu tiên Classic Editor: luôn mở bằng HTML mode, loại bỏ các thứ chỉ phục vụ block editor.
- Làm sạch
<head>: bỏ wp_generator, wlwmanifest, rsd, shortlink, các inline CSS rác từ Global Styles (WP 6.x).
Tất cả được gói trong một snippet duy nhất, dùng cho các site thuần Classic, theme custom, hoặc theme tự code không phụ thuộc Gutenberg.
2. Đoạn mã tối ưu cho Classic Theme
Thêm đoạn mã dưới đây vào file functions.php của child theme hoặc plugin riêng của bạn:
<?php
/**
* Classic Theme Optimization Pack
* Clean up admin UI and frontend output for classic WordPress sites.
*/
// ==================================================
// Admin UI cleanup
// ==================================================
/**
* Hide admin submenus (e.g. Dashboard > Updates)
*/
add_action('admin_menu', function () {
remove_submenu_page('index.php', 'update-core.php');
}, 999);
/**
* Remove Theme File Editor from Appearance menu
*/
add_action('_admin_menu', function () {
remove_action('admin_menu', '_add_themes_utility_last', 101);
}, 1);
/**
* Disable autosave script in post editor
*/
add_action('wp_print_scripts', function () {
if (is_admin()) {
wp_deregister_script('autosave');
}
});
/**
* Hide help tabs on admin screens
*/
add_action('admin_head', function () {
if (function_exists('get_current_screen')) {
$screen = get_current_screen();
if (method_exists($screen, 'remove_help_tabs')) {
$screen->remove_help_tabs();
}
}
});
/**
* Clean up admin bar (remove WP logo and links)
*/
add_action('wp_before_admin_bar_render', function () {
global $wp_admin_bar;
if (!is_admin_bar_showing() || !is_object($wp_admin_bar)) {
return;
}
foreach (['wp-logo', 'wporg', 'documentation', 'support-forums', 'feedback'] as $id) {
$wp_admin_bar->remove_menu($id);
}
});
/**
* Remove news widgets from Dashboard
*/
add_action('admin_init', function () {
remove_meta_box('dashboard_primary', 'dashboard', 'side');
remove_meta_box('dashboard_secondary', 'dashboard', 'side');
});
/**
* Customize login page header URL, title and hide language dropdown
*/
add_filter('login_headerurl', fn() => home_url());
add_filter('login_headertext', fn() => get_bloginfo('name'));
add_filter('login_display_language_dropdown', '__return_false');
// ==================================================
// Frontend cleanup
// ==================================================
/**
* Disable Nextend Social Login CSS/JS on frontend when user is logged in
*/
add_action('wp', function () {
if (is_user_logged_in() && !is_admin() && class_exists('NextendSocialLogin')) {
remove_action('wp_head', ['NextendSocialLogin', 'styles'], 100);
remove_action('wp_print_scripts', ['NextendSocialLogin', 'nslDOMReady']);
remove_action('wp_print_footer_scripts', ['NextendSocialLogin', 'scripts'], 100);
}
});
/**
* Deregister jQuery on frontend (only if your theme/plugins do not need it)
*/
add_action('wp_enqueue_scripts', function () {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_deregister_script('jquery-core');
wp_deregister_script('jquery-migrate');
}
}, 0);
/**
* Disable emoji scripts and styles
*/
add_action('init', function () {
$emoji_actions = [
['admin_print_styles', 'print_emoji_styles'],
['wp_head', 'print_emoji_detection_script', 7],
['admin_print_scripts', 'print_emoji_detection_script'],
['wp_print_styles', 'print_emoji_styles'],
];
foreach ($emoji_actions as $a) {
remove_action(...$a);
}
remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
remove_filter('the_content_feed', 'wp_staticize_emoji');
remove_filter('comment_text_rss', 'wp_staticize_emoji');
add_filter('tiny_mce_plugins', function ($plugins) {
return is_array($plugins) ? array_diff($plugins, ['wpemoji']) : [];
});
});
/**
* Disable classic-theme-styles and intrinsic styles (WP 6.x)
*/
remove_action('wp_enqueue_scripts', 'wp_enqueue_classic_theme_styles');
add_action('wp_enqueue_scripts', function () {
wp_dequeue_style('classic-theme-styles');
}, 20);
/**
* Disable Gutenberg block editor for all post types
*/
add_filter('use_block_editor_for_post_type', '__return_false', 10);
/**
* Remove block library CSS on frontend
*/
add_action('wp_enqueue_scripts', function () {
foreach (['wp-block-library', 'wp-block-library-theme', 'wc-block-style', 'storefront-gutenberg-blocks'] as $style) {
wp_dequeue_style($style);
}
}, 100);
/**
* Remove WordPress version from meta generator
*/
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_empty_string');
/**
* Optionally remove version query string from scripts/styles
*
* Uncomment if you really want to strip ?ver= from assets.
*/
// add_filter('style_loader_src', 'init_manga_remove_ver', 9999);
// add_filter('script_loader_src', 'init_manga_remove_ver', 9999);
// function init_manga_remove_ver($src) {
// return strpos($src, 'ver=') !== false ? remove_query_arg('ver', $src) : $src;
// }
/**
* Disable all RSS/Atom feeds and redirect to homepage
*/
add_action('do_feed', 'init_manga_disable_feeds', -1);
add_action('do_feed_rdf', 'init_manga_disable_feeds', -1);
add_action('do_feed_rss', 'init_manga_disable_feeds', -1);
add_action('do_feed_rss2', 'init_manga_disable_feeds', -1);
add_action('do_feed_atom', 'init_manga_disable_feeds', -1);
add_action('do_feed_rss2_comments', 'init_manga_disable_feeds', -1);
add_action('do_feed_atom_comments', 'init_manga_disable_feeds', -1);
add_action('feed_links_show_posts_feed', '__return_false', -1);
add_action('feed_links_show_comments_feed', '__return_false', -1);
remove_action('wp_head', 'feed_links', 2);
remove_action('wp_head', 'feed_links_extra', 3);
function init_manga_disable_feeds() {
wp_redirect(home_url());
exit;
}
/**
* Disable oEmbed and related scripts/meta
*/
add_action('init', fn() => wp_deregister_script('wp-embed'));
remove_action('rest_api_init', 'wp_oembed_register_route');
add_filter('embed_oembed_discover', '__return_false');
remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
remove_action('wp_head', 'wp_oembed_add_discovery_links');
remove_action('wp_head', 'wp_oembed_add_host_js');
/**
* Remove block Global Styles CSS (handle: global-styles)
*/
add_action('wp_enqueue_scripts', fn() => wp_dequeue_style('global-styles'), 100);
/**
* Default to HTML editor in Classic Editor
*/
add_filter('wp_default_editor', fn() => 'html');
/**
* Remove wlwmanifest and RSD links from head
*/
remove_action('wp_head', 'rsd_link');
remove_action('wp_head', 'wlwmanifest_link');
/**
* Remove shortlink tag from head
*/
remove_action('wp_head', 'wp_shortlink_wp_head', 10, 0);
/**
* Remove Global Styles inline CSS (WP 6.9+)
*/
remove_action('wp_enqueue_scripts', 'wp_enqueue_global_styles');
remove_action('wp_footer', 'wp_enqueue_global_styles', 1);
3. Lưu ý quan trọng trước khi dùng
- Không nên dùng cho Full Site Editing / Block Theme: đoạn mã này tắt Gutenberg, block CSS, Global Styles, classic-theme-styles… nên theme block sẽ gần như “tan nát”.
- Không nên dùng nếu bạn cần RSS: phần RSS bị tắt hoàn toàn và redirect về trang chủ. Nếu bạn vẫn cần feed cho SEO, podcast, hoặc tích hợp bên thứ ba, hãy bỏ đoạn
init_manga_disable_feeds(). - Cẩn thận khi tắt jQuery: chỉ giữ lại phần “Remove jQuery” nếu bạn chắc chắn theme và plugin của bạn không lệ thuộc jQuery trên frontend.
- Embed và oEmbed: nếu bạn thường xuyên nhúng video YouTube, tweet, post Facebook bằng cách dán URL, không nên tắt phần Embed.
- Autosave và help tab: nếu site có nhiều tác giả non tay, autosave và help tab đôi khi vẫn hữu ích; bạn có thể xóa riêng các đoạn liên quan.
4. Cách áp dụng an toàn
- Luôn dùng trên child theme hoặc plugin riêng, tránh sửa trực tiếp theme mua trên marketplace.
- Commit vào Git (nếu có) rồi bật từng nhóm nhỏ, kiểm tra lại:
- Đăng nhập admin, thử editor, Dashboard, admin bar.
- Kiểm tra frontend: form, slider, popup, social login, bình luận…
- Xem lại source HTML để chắc chắn Global Styles, block CSS, emoji, wp-embed… đã biến mất như mong muốn.
- Nếu có lỗi layout hoặc JS, rollback hoặc tạm thời comment từng phần (jQuery, embed, RSS) để xác định chính xác thứ gây lỗi.
Kết luận
Đối với các website thuần Classic Theme, không dùng Gutenberg, việc gom các tối ưu “dọn rác” WordPress vào một đoạn mã duy nhất giúp bạn kiểm soát frontend và admin gọn gàng, sạch sẽ, dễ bảo trì. Bạn không cần phải cài thêm 3–4 plugin “disable this, remove that” chỉ để tắt những thứ WordPress tự thêm vào. Chỉ cần hiểu site của mình cần gì, bật đúng phần, tắt phần thừa – website sẽ nhẹ hơn, sạch hơn, ít bug vặt hơn.
Bình luận