Giới Thiệu 10+ Chức Năng Hữu Ích Cho WordPress
File functions.php
trong WordPress đóng vai trò quan trọng trong việc thêm các tính năng tùy chỉnh vào website mà không cần cài thêm plugin. Dưới đây là 10+ đoạn code hữu ích giúp bạn tăng tốc website, bảo mật và tối ưu trải nghiệm người dùng.
1. Tắt XML-RPC Để Ngăn Chặn Tấn Công
Mặc định, WordPress bật XML-RPC, nhưng nếu bạn không sử dụng, hãy tắt để tăng cường bảo mật:
1 | add_filter('xmlrpc_enabled', '__return_false'); |
👉 Lợi ích: Ngăn chặn tấn công brute force qua XML-RPC, giảm tải server.
2. Xóa Phiên Bản WordPress Để Tránh Hackers
Hacker có thể khai thác lỗi bảo mật dựa trên phiên bản WordPress của bạn. Hãy ẩn nó:
1 2 3 4 | function remove_wp_version_info() { return ''; } add_filter('the_generator', 'remove_wp_version_info'); |
👉 Lợi ích: Tăng cường bảo mật, tránh bị hacker quét lỗi từ phiên bản WP.
3. Vô Hiệu Hóa Chỉnh Sửa File Trong Admin
Tránh tình trạng hacker truy cập Admin và chỉnh sửa file theme/plugin bằng cách tắt chức năng này:
1 | define('DISALLOW_FILE_EDIT', true); |
4. Tự Động Chuyển Hướng Sau Khi Đăng Nhập
Tùy chỉnh trang đích sau khi đăng nhập:
1 2 3 4 5 6 7 8 9 10 11 | function redirect_users_after_login($redirect_to, $request, $user) { if (isset($user->roles) && is_array($user->roles)) { if (in_array('administrator', $user->roles)) { return admin_url(); // Admin về trang quản trị } else { return home_url(); // Người dùng thường về trang chủ } } return $redirect_to; } add_filter('login_redirect', 'redirect_users_after_login', 10, 3); |
5. Tự Động Đặt Ảnh Đại Diện Cho Bài Viết
Nếu quên đặt ảnh đại diện, code này sẽ tự động chọn ảnh đầu tiên làm thumbnail:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | function auto_set_featured_image() { global $post; if (!has_post_thumbnail($post->ID)) { $attached_image = get_children([ 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => 1 ]); if ($attached_image) { foreach ($attached_image as $attachment_id => $attachment) { set_post_thumbnail($post->ID, $attachment_id); } } } } add_action('save_post', 'auto_set_featured_image'); |
6. Xóa Emoji Để Tăng Tốc WordPress
Emoji có thể làm chậm tốc độ load trang. Nếu không cần dùng, hãy xóa đi:
1 2 3 4 5 6 7 8 9 10 | 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_action('init', 'disable_wp_emojis'); |
👉 Lợi ích: Tăng tốc độ tải trang, đặc biệt hữu ích cho SEO.
7. Thêm Google Fonts Vào WordPress
1 2 3 4 | function add_google_fonts() { wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;700&display=swap', false); } add_action('wp_enqueue_scripts', 'add_google_fonts'); |
8. Thêm Class CSS Tùy Chỉnh Cho Menu WordPress
Nếu muốn thêm class vào menu, hãy sử dụng đoạn code sau:
1 2 3 4 5 | function add_menu_link_class($atts, $item, $args) { $atts['class'] = 'custom-menu-class'; return $atts; } add_filter('nav_menu_link_attributes', 'add_menu_link_class', 10, 3); |
👉 Lợi ích: Giúp dễ dàng tùy chỉnh menu bằng CSS.
9. Tự Động Thêm Nnoopener Noreferrer Vào Liên Kết
Bảo mật website khi mở liên kết trong tab mới:
1 2 3 4 | function modify_link_target($content) { return str_replace('target="_blank"', 'target="_blank" rel="noopener noreferrer"', $content); } add_filter('the_content', 'modify_link_target'); |
10. Xóa /category/ Trong URL WordPress
Nếu bạn muốn làm đẹp đường dẫn chuyên mục, hãy sử dụng đoạn code này:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | function remove_category_slug($wp_rewrite) { $wp_rewrite->rules = array_merge( ['(.+)/?$' => 'index.php?category_name=$matches[1]'], $wp_rewrite->rules ); } add_filter('category_rewrite_rules', 'remove_category_slug'); function custom_category_redirect() { if (strpos($_SERVER['REQUEST_URI'], '/category/') === false) { return; } wp_redirect(str_replace('/category/', '/', $_SERVER['REQUEST_URI']), 301); exit; } add_action('template_redirect', 'custom_category_redirect'); |
Kết Luận
Trên đây là 10+ đoạn code hữu ích cho functions.php giúp website WordPress của bạn nhanh hơn, bảo mật hơn và chuẩn SEO hơn. Nếu bạn cần tối ưu thêm, hãy để lại bình luận nhé! 🚀
- Sharecode cách code chống spam đơn hàng bằng OTP gmail vào WooCommerce
- Một số cách đưa toàn bộ JavaScript xuống Footer
- Thủ thuật wordpress – Code thêm nút Edit nhanh cho Admin trên Mobile WordPress
- Hướng dẫn Cách Tối ưu SEO Danh Mục Sản Phẩm Website WordPress
- Flash sale toàn bộ theme wordpress bán hàng đến 50%