Bạn sẽ nhận được gì
- Tự động gửi embed lên Discord khi bài viết (
post) được đăng lần đầu. - Hiển thị ảnh đại diện bài viết kích thước
fulltrong embed. - Tùy biến tiêu đề, màu sắc, footer, username bot.
Yêu cầu
- WordPress có quyền thêm “Code Snippets” (plugin Code Snippets hoặc functions.php của child theme).
- Một Discord Webhook URL (tạo trong: Server Settings → Integrations → Webhooks).
Cơ chế hoạt động
Khi trạng thái bài viết chuyển từ không-public → publish lần đầu, hook transition_post_status sẽ kích hoạt. Snippet kiểm tra post type, bỏ qua autosave/revision, lấy tiêu đề/URL/Featured Image rồi gửi payload JSON đến Discord Webhook.
Cài đặt & sử dụng
- Mở Code Snippets → Add New (hoặc thêm vào
functions.phpcủa child theme). - Dán đoạn code dưới, thay Webhook URL của bạn vào hằng
INIT_DISCORD_WEBHOOK. - Lưu và kích hoạt. Đăng một bài viết mới để kiểm tra.
Snippet hoàn chỉnh
// Replace with your Discord Webhook URL
if (!defined('INIT_DISCORD_WEBHOOK')) {
define('INIT_DISCORD_WEBHOOK', 'https://discord.com/api/webhooks/XXX/YYY'); // <-- gắn link webhook của bạn tại đây
}
/**
* Fire once when a 'post' is published (first time).
*/
add_action('transition_post_status', function ($new_status, $old_status, $post) {
if ($new_status !== 'publish' || $old_status === 'publish') return;
if ($post->post_type !== 'post') return;
if (wp_is_post_autosave($post) || wp_is_post_revision($post)) return;
$post_id = $post->ID;
$title = get_the_title($post_id);
$url = get_permalink($post_id);
$blogname = get_bloginfo('name');
// Lấy Featured Image cỡ full (ưu tiên ảnh lớn hiển thị trong embed)
$thumb_id = get_post_thumbnail_id($post_id);
$image_url = $thumb_id ? wp_get_attachment_image_url($thumb_id, 'full') : null;
$embed = [
'title' => 'Bài viết mới đang đợi bạn!', // Tuỳ chỉnh tuỳ ý (VD: "New post published")
'description' => $title ?: 'Untitled',
'url' => $url,
'timestamp' => gmdate('c'),
'color' => 0x5865F2, // Discord blurple
'footer' => ['text' => $blogname],
];
if ($image_url) {
$embed['image'] = ['url' => $image_url]; // Dùng ảnh full-size
}
$payload = [
'embeds' => [ $embed ],
'username' => $blogname . ' Bot',
// 'avatar_url' => get_site_icon_url() ?: '', // Bật nếu muốn avatar theo site icon
];
wp_remote_post(INIT_DISCORD_WEBHOOK, [
'headers' => ['Content-Type' => 'application/json'],
'body' => wp_json_encode($payload),
'timeout' => 10,
]);
}, 10, 3);
Kiểm tra nhanh
- Đăng một bài viết mới (không dùng Quick Edit để đổi trạng thái).
- Đảm bảo có Featured Image để embed hiển thị ảnh lớn.
- Mở Discord channel chứa webhook để xem thông báo.
Tùy biến thường dùng
- Đổi tiêu đề embed: thay chuỗi
Yeni Seri yayınlandıthànhNew post published(hoặc bản địa hoá bạn muốn). - Đặt avatar bot theo Site Icon: bỏ comment dòng
'avatar_url' => get_site_icon_url(). - Đổi màu viền embed: thay
0x5865F2bằng mã hex khác (ví dụ0x00B894). - Giới hạn theo category: chèn kiểm tra trước khi gửi:
if ( ! has_term( ['news','release'], 'category', $post_id ) ) return; - Mention một role: thêm trường
contentvàallowed_mentions:$payload['content'] = '<@&ROLE_ID>'; // thay ROLE_ID $payload['allowed_mentions'] = ['parse' => ['roles']];
Bảo mật & lưu ý
- Webhook là bí mật. Tránh public mã nguồn chứa URL trên repo công khai.
- Có thể chuyển URL vào
wp-config.phphoặc biến môi trường, rồidefine()từ đó. - Nếu không thấy thông báo: kiểm tra lỗi mạng hosting, tường lửa/ModSecurity, hoặc quyền gửi ra ngoài.
FAQ ngắn
Hỏi: Sửa bài có gửi lại không?
Đáp: Không. Chỉ khi đăng lần đầu (old_status != publish, new_status == publish).
Hỏi: Không có Featured Image thì sao?
Đáp: Embed vẫn gửi, chỉ thiếu phần hình ảnh.
Kết luận
Một snippet nhẹ, gắn là chạy: publish bài viết → Discord bật thông báo đẹp, kèm ảnh full. Từ đây bạn có thể mở rộng logic (lọc category, mention role, brand màu, avatar bot) theo đúng nhu cầu thực tế. Triển thôi!
Admin
19/12/2025 lúc 22:35
đã có plugin với chức năng này: Init Pulse For Discord