WordPress + Server酱:新评论/订单/注册推送到微信

效果:WordPress 产生新评论、新订单或新用户注册时,自动收到微信通知。

前置条件

  • Server酱 SendKey(30 秒获取教程
  • WordPress 站点管理员权限,可编辑主题文件或安装插件
  • 如需推送到企业微信/钉钉/飞书群,参见通道配置

配置步骤

方式一:functions.php 钩子直连(无需插件)

将以下代码加入主题的 functions.php,新评论提交时即推送通知:

add_action('comment_post', function($comment_id, $comment_approved, $commentdata) {
    $post = get_post($commentdata['comment_post_ID']);
    $title = '【新评论】' . $post->post_title;
    $desp = "评论者:{$commentdata['comment_author']}\n\n"
          . "内容:{$commentdata['comment_content']}\n\n"
          . "链接:" . get_permalink($post->ID);

    wp_remote_post('https://sctapi.ftqq.com/你的SENDKEY.send', [
        'body' => [
            'title' => $title,
            'desp'  => $desp,
        ],
    ]);
}, 10, 3);

你的SENDKEY 替换为实际 SendKey。若使用 Server酱³(SendKey 以 sctp 开头),API 地址改为 https://你的UID.push.ft07.com/send/你的SENDKEY.send,UID 是 SendKey 中 sctpt 之间的数字。

新用户注册等事件可用同样方式挂钩对应钩子,调用 wp_remote_post 发送到 Server酱 API。

方式二:WP Webhooks 插件配置

  1. 在 WordPress 后台安装并启用 WP Webhooks 插件
  2. 创建新 Webhook,选择触发器(如 New comment 或 WooCommerce 新订单)
  3. 将 Send Data 的目标 URL 设为 https://sctapi.ftqq.com/你的SENDKEY.send
  4. 请求方式选 POST,将字段映射到 titledesp 两个参数

具体插件界面以应用内实际显示为准。

示例代码

以下代码处理新评论通知,可直接粘贴到 functions.php

// 新评论通知
add_action('comment_post', function($comment_id, $comment_approved, $commentdata) {
    $post = get_post($commentdata['comment_post_ID']);
    $title = '【新评论】' . $post->post_title;
    $desp = "评论者:{$commentdata['comment_author']}\n\n"
          . "内容:{$commentdata['comment_content']}\n\n"
          . "链接:" . get_permalink($post->ID);

    wp_remote_post('https://sctapi.ftqq.com/你的SENDKEY.send', [
        'body' => [
            'title' => $title,
            'desp'  => $desp,
        ],
    ]);
}, 10, 3);

其他事件(如新用户注册)可参照此模式挂钩对应钩子。

常见问题

免费额度够用吗?

免费版每天 5 条,每分钟上限 50 条。评论量大的站点可在代码中加条件过滤(如只推送待审核评论)。订阅会员目前特价最低 3 元/月,一天最多可发 1000 条(价格详情)。

消息没收到怎么办?

先检查 SendKey 是否正确、API 地址是否匹配 Key 类型(SCT 开头用 sctapi.ftqq.com,sctp 开头用 ft07.com 域名)。更多排查步骤见常见问题与排查

高频评论会不会触发频率限制?

Server酱 每分钟上限 50 条。可在 comment_post 回调中加 if ($comment_approved === 'spam') return; 过滤垃圾评论,减少无效推送。

WP Webhooks 和 functions.php 选哪个?

functions.php 钩子方式无需安装插件,适合熟悉 PHP 的开发者;WP Webhooks 插件适合不写代码、通过界面配置触发器和字段映射的场景。

相关集成

懒人方案:把这段话复制给你的 AI 助手

把下面这段文字原样粘贴给 Claude Code、Cursor 或任何 AI 编程助手,它就会替你完成 Server酱 的接入:

请帮我把 Server酱 微信通知接入到我当前的项目/工具中:
1. API:POST https://sctapi.ftqq.com/{SendKey}.send,参数 title(必填,不能含换行)、desp(正文,支持 Markdown,可选);成功时返回 JSON 的 code 为 0。
2. 如果我的 SendKey 以 sctp 开头,则改用 https://{uid}.push.ft07.com/send/{SendKey}.send,uid 是 SendKey 中 sctp 与 t 之间的数字。
3. SendKey 从环境变量 SERVERCHAN_SENDKEY 读取;如果没有,提醒我到 https://sct.ftqq.com 免费获取(每天 5 条额度)。
4. 安全要求:不要把 SendKey 硬编码进代码、不要打印到日志、不要提交到 git。
5. 频率约定:任务结束发 1 条结果摘要即可,批量任务合并成一条,不要每步都推。
请根据我当前的项目和场景直接写好代码/配置并验证。