不仅要通知有新评论,而且要把新评论的地址给带上,要不然进博客再找比较麻烦,不够人性化
Server酱的获取见官网或文献[1-2]
其实很简单,拼接字符串+调用server酱接口,,只是对php不熟,过程比较曲折
在 外观-主题编辑器-模板函数 (functions.php) 中 (或自行找到该文件修改)添加: (把代码中的【your.ip.or.domain】和【Server酱SCKEY】换成你的)
//新评论微信推送
//该评论地址格式为:http://your.ip.or.domain/?p=10#comment-5 下面主要是拼接这样的地址
//$text+=$comment->comment_post_title; //这样加 结果就是0 这不是php的字符串拼接 而是: "9"+"123"="132" 字符串拼接是"."
function new_comment_send($comment_id)
{
$text = '新评论来辣';
$comment = get_comment($comment_id);
$comment_author=$comment->comment_author;
$postid=$comment->comment_post_ID;
$post = get_post($postid);
$post_title=$post->post_title;
$desp = "http://your.ip.or.domain/?p=".$postid."#comment-".$comment_id; //使用"."进行字符串拼接
$desp="文章标题:".$post_title."\n评论内容:".$comment->comment_content."\n作者:".$comment_author."\n地址:".$desp;
$key = 'Server酱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);
$result = file_get_contents('http://sc.ftqq.com/'.$key.'.send', false, $context);
}
add_action('comment_post', 'new_comment_send', 19, 2);
其中get_comment、get_post都是WordPress里的常用函数,$post和$comment包含的属性可以在mysql数据库相应的表中查看
最后一句解释:WordPress里新评论的钩子是comment_post 这一句是把自定义的新评论处理函数new_comment_send添加到comment_post钩子上 后面两个数字分别是参数$priority、$accepted_args的值
add_action('comment_post', 'new_comment_send', 19, 2);
参考文献: