1. Đưa toàn bộ JavaScript xuống footer
Mặc định, WordPress tải JavaScript trong phần <head>. Tuy nhiên, việc chuyển script xuống <footer> sẽ giúp trang hiển thị nhanh hơn. Dưới đây là cách thực hiện:
function move_js_to_footer() {
remove_action('wp_head', 'wp_print_scripts');
remove_action('wp_head', 'wp_print_head_scripts', 9);
remove_action('wp_head', 'wp_enqueue_scripts', 1);
add_action('wp_footer', 'wp_print_scripts', 5);
add_action('wp_footer', 'wp_enqueue_scripts', 5);
add_action('wp_footer', 'wp_print_head_scripts', 5);
}
add_action('wp_enqueue_scripts', 'move_js_to_footer');
2. Tắt tính năng Emoji
Emoji là tính năng ít dùng với đa số site, nhưng lại tải thêm JS/CSS không cần thiết. Để tắt:
function disable_wp_emojis() {
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');
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('tiny_mce_plugins', function($plugins) {
return is_array($plugins) ? array_diff($plugins, ['wpemoji']) : [];
});
}
add_action('init', 'disable_wp_emojis');
3. Sử dụng jQuery từ Google Hosted Libraries
Thay vì dùng jQuery nội bộ, bạn có thể lấy từ Google để tận dụng cache trình duyệt:
function use_jquery_google() {
if (!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js', [], null, true);
wp_enqueue_script('jquery');
}
}
add_action('init', 'use_jquery_google');
4. Ẩn phiên bản WordPress
Giấu thông tin phiên bản để giảm rủi ro bị khai thác lỗ hổng bảo mật:
remove_action('wp_head', 'wp_generator');
add_filter('the_generator', '__return_empty_string');
function remove_version_from_assets($src) {
return strpos($src, 'ver=') ? remove_query_arg('ver', $src) : $src;
}
add_filter('style_loader_src', 'remove_version_from_assets', 9999);
add_filter('script_loader_src', 'remove_version_from_assets', 9999);
5. Vô hiệu hóa Gutenberg và các file block CSS
Nếu bạn chỉ dùng Classic Editor, hãy tắt Gutenberg và CSS liên quan:
add_filter('use_block_editor_for_post_type', '__return_false');
function remove_gutenberg_styles() {
wp_dequeue_style('wp-block-library');
wp_dequeue_style('wp-block-library-theme');
wp_dequeue_style('wc-block-style'); // WooCommerce
wp_dequeue_style('storefront-gutenberg-blocks'); // Theme Storefront
}
add_action('wp_enqueue_scripts', 'remove_gutenberg_styles', 100);
6. Tắt toàn bộ RSS Feed
Nếu website không phải blog hoặc không cần feed, có thể tắt toàn bộ RSS:
function disable_all_feeds() {
wp_redirect(home_url());
exit;
}
add_action('do_feed', 'disable_all_feeds', -1);
add_action('do_feed_rdf', 'disable_all_feeds', -1);
add_action('do_feed_rss', 'disable_all_feeds', -1);
add_action('do_feed_rss2', 'disable_all_feeds', -1);
add_action('do_feed_atom', 'disable_all_feeds', -1);
add_action('do_feed_rss2_comments', 'disable_all_feeds', -1);
add_action('do_feed_atom_comments', 'disable_all_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);
7. Tắt oEmbed để giảm script và REST route
Nếu không dùng tính năng nhúng nội dung từ URL (YouTube, Twitter…), có thể tắt:
function disable_oembed_everything() {
remove_action('rest_api_init', 'wp_oembed_register_route');
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');
add_filter('embed_oembed_discover', '__return_false');
}
add_action('init', 'disable_oembed_everything');
function stop_loading_embed_script() {
wp_deregister_script('wp-embed');
}
add_action('init', 'stop_loading_embed_script');
8. Loại bỏ Global Styles
Trên WordPress 5.9+, hệ thống chèn CSS Global Styles không cần thiết nếu không dùng Block Theme:
function remove_global_styles() {
wp_dequeue_style('global-styles');
}
add_action('wp_enqueue_scripts', 'remove_global_styles', 100);
Kết luận
Các thủ thuật trên giúp bạn kiểm soát chặt chẽ hơn những gì được nạp vào website, từ đó giảm tải tài nguyên, tăng tốc độ, giảm rủi ro bảo mật và giữ WordPress nhẹ nhàng hơn. Hãy cân nhắc áp dụng tùy theo tính chất website bạn đang vận hành.
Bình luận