Các hook ít ai biết nhưng cực kỳ hữu dụng trong WordPress

WordPress có hàng ngàn hook (action và filter) giúp bạn mở rộng chức năng một cách tinh tế mà không sửa trực tiếp core. Ngoài những cái quen thuộc như init, wp_head, the_content, vẫn còn nhiều hook ít được nhắc đến nhưng rất mạnh mẽ. Dưới đây là danh sách các hook “ẩn mình” nhưng cực kỳ hữu ích trong thực chiến.

Các hook ít ai biết nhưng cực kỳ hữu dụng trong WordPress

1. wp_mail_failed – Bắt lỗi khi gửi mail thất bại

Hook này giúp bạn ghi log hoặc xử lý khi một email trong WordPress không gửi được.

add_action('wp_mail_failed', function($wp_error) {
    error_log('Lỗi gửi mail: ' . $wp_error->get_error_message());
});

2. user_contactmethods – Tùy biến thông tin người dùng

Dùng hook này để thêm/xóa trường liên hệ trong hồ sơ người dùng.

add_filter('user_contactmethods', function($methods) {
    $methods['zalo'] = 'Zalo';
    unset($methods['yim']); // Xóa Yahoo Messenger
    return $methods;
});

3. heartbeat_send – Thêm dữ liệu vào heartbeat API

Cho phép bạn gửi dữ liệu tùy chỉnh từ trình duyệt về server định kỳ qua Heartbeat.

add_filter('heartbeat_send', function($data) {
    $data['my_custom_info'] = 'Ping từ client';
    return $data;
});

4. plugin_action_links_{$plugin_file} – Thêm liên kết tùy chỉnh trong danh sách plugin

Hook này giúp bạn thêm liên kết “Cài đặt”, “Trợ giúp”,… ngay dưới tên plugin.

add_filter('plugin_action_links_my-plugin/my-plugin.php', function($links) {
    $links[] = '<a href="options-general.php?page=my-plugin-settings">Cài đặt</a>';
    return $links;
});

5. wp_unique_post_slug_is_bad_flat_slug – Ngăn chặn slug cụ thể

Dùng để cấm người dùng đặt slug trùng với trang đặc biệt như “admin”, “login”.

add_filter('wp_unique_post_slug_is_bad_flat_slug', function($is_bad, $slug) {
    $reserved = ['admin', 'login', 'wp-json'];
    return in_array($slug, $reserved) ? true : $is_bad;
}, 10, 2);

6. pre_user_login – Tự động xử lý tên đăng nhập khi tạo user

add_filter('pre_user_login', function($login) {
    return strtolower(sanitize_user($login)); // Chuyển về chữ thường
});

7. script_loader_tag – Thêm thuộc tính defer, async vào script

add_filter('script_loader_tag', function($tag, $handle, $src) {
    if ($handle === 'my-heavy-script') {
        return '<script src="' . $src . '" defer></script>';
    }
    return $tag;
}, 10, 3);

8. wp_image_editors – Thay đổi trình xử lý ảnh mặc định

add_filter('wp_image_editors', function($editors) {
    return ['WP_Image_Editor_Imagick', 'WP_Image_Editor_GD'];
});

9. auto_update_plugin – Kiểm soát update tự động plugin cụ thể

add_filter('auto_update_plugin', function($update, $item) {
    if ($item->slug === 'akismet') return false;
    return $update;
}, 10, 2);

10. redirect_canonical – Ngăn WordPress redirect không mong muốn

add_filter('redirect_canonical', function($redirect_url) {
    if (is_singular('custom_post')) return false;
    return $redirect_url;
});

Kết luận

Những hook trên thường bị “bỏ quên” nhưng cực kỳ hữu dụng trong việc tinh chỉnh site WordPress một cách thông minh và hiệu quả. Hãy lưu lại và áp dụng dần để site của bạn vừa nhẹ, vừa linh hoạt mà không cần tới plugin nặng nề.

Bình luận


  • Không có bình luận.

Init Toolbox

Nhấn Ctrl + \ trên máy tính, hoặc vuốt sang trái ở bất kỳ đâu trên mobile.

Đăng nhập





Đang tải...