不僅要通知有新評論,而且要把新評論的地址給帶上,要不然進博客再找比較麻煩,不夠人性化
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);
參考文獻: