博客评论发现回复不及时即会导致网站热度下降,网上有许多做法实现,不少网站是通过邮件来提醒的,但是依旧不够及时,所以我们使用微信进行提醒。
实现微信提醒就需要借助Server酱
「Server酱」,英文名「ServerChan」,是一款「程序员」和「服务器」之间的通信软件。
说人话?就是从服务器推报警和日志到手机的工具。
开通并使用上它,只需要一分钟:
- 登入:用GitHub账号登入网站,就能获得一个SCKEY(在「发送消息」页面)
- 绑定:点击「微信推送」,扫码关注同时即可完成绑定
- 发消息:往 http://sc.ftqq.com/SCKEY.send 发GET请求,就可以在微信里收到消息啦
来个示意图:
具体过程
1、注册 GitHub 账号(没有账号的注册有的跳过)
2、打开 Server酱 官网(地址在文末或者自行百度),点击右上角的『登入』按钮并用 GitHub 账号登入网站,然后点击『发送信息』进入页面就能获得一个 SCKEY。(如下图)
3、绑定:点击「微信推送」,扫码关注同时即可完成绑定
整合wordpress
你当前使用的wordpress主题中 functions.php,打开functions.php最后一行是“?>”,把下面的内容加入这一行的上面。记得替换你的 SCKEY。
//评论微信推送 function sc_send($comment_id) { $text = '博客上有一条新的评论'; $comment = get_comment($comment_id); $desp = $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);
微信推送新评论效果
完成上面操作之后,当博客有新评论的时候,微信就会收到提醒,速度很快基本上没有延时。
微信端效果
But 有一个问题,推送内容只能看到评论的内容,站长和管理员看不到评论的是哪一篇文章。
实现可查具体文章 ID
需要在上述代码中添加文章ID 进去 $comment->comment_post_ID
//评论微信推送 function sc_send($comment_id) { $text = '博客上有一条新的评论'; $comment = get_comment($comment_id); $desp = $comment->comment_post_ID.$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);
再来看一下效果
这样我们就知道是 https://www.lurending.com/1102.html 这篇文章有了新的评论。
如果主题固定链接设置的不是 /%post_id%.html ,可以使用$comment->comment_post_title 获取文章标题。推送的内容里就会有文章标题,你就知道被评论的文章是哪一篇了。
至此结束,功能非必须,但是……
还可以的啦 ✗微笑✗
评论。