0%

微信推送WordPress新评论通知

几个月没打理博客,登录到后台后发现多了几个评论,但很遗憾都已经是数月之前的了,所以没能及时回复这些朋友们。 前阵子接触到了"Server酱",很有用的小工具,可以通过简单的http post达到向自己的微信推送通知的目的。 那能不能在wordpress有新评论时推送通知?

研究了一下发现wordpress没有内置这样的hook功能,网上有不少方法,修改主题的functions.php页面,在最后添加下列代码就可以了。 这段代码网上有不少文章提到,也不知道原出处,总之向所有相关的大佬们致敬。

20190310更新,在发消息前先判断评论内容是否含有中文,若无中文则不提醒。这段时间被垃圾评论烦到不行,wordpress的验证码插件太弱了也没起到太多的作用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
/**
* 基于“Server酱”的新评论微信推送
*/
function sc_send($comment_id)
{
$comment = get_comment($comment_id);

# 判断是否含有中文
if(!preg_match("/[\x7f-\xff]/", $comment->comment_content)){
return;
}

$text = 'Spirit blog-上有新的评论';
$desp ='《'.get_the_title($comment->comment_post_ID) .'》, '
.'评论者:'.$comment->comment_author.', '
.'评论内容:'. $comment->comment_content;
$key = '你的SCKEy';
$postdata = http_build_query(
array(
'text' => $text,
'desp' => $desp
)
);
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'content' => $postdata
)
);
$context = stream_context_create($opts);
return $result = file_get_contents('http://sc.ftqq.com/'.$key.'.send', false, $context);
}

add_action('comment_post', 'sc_send', 19, 2);

参考文章: