超簡(jiǎn)單!兩步實(shí)現(xiàn)Wordpress評(píng)論微信通知~
戳上方“執(zhí)行上下文”,選擇“置頂公眾號(hào)”
關(guān)鍵時(shí)刻,第一時(shí)間送達(dá)!

WordPress收到評(píng)論的時(shí)候可以通過(guò)郵件發(fā)送評(píng)論通知,但是郵件通知的可能不是那么能及時(shí)的查看,所以增加一個(gè)微信通知。
實(shí)現(xiàn)方式:
1、第一種就是通過(guò)企業(yè)微信群聊機(jī)器人來(lái)通知。
2、第二種就是通過(guò)server醬來(lái)實(shí)現(xiàn)通知
這兩種方式各有好壞,通過(guò)server醬通知的方式很多,選擇性很多。但是畢竟是用的別人的服務(wù),涉及安全,穩(wěn)定以及需要會(huì)員的問(wèn)題。用自己的企業(yè)微信來(lái)通知就不需要考慮服務(wù)不可用的問(wèn)題,通知頻率也不會(huì)限制。缺點(diǎn)是選擇性就一個(gè)。
代碼:
企業(yè)微信版本:
date_default_timezone_set("Asia/Shanghai");
//?請(qǐng)求體
function?request_post($url?=?'',?$post_data?=?array(),$dataType='')?{
?if?(empty($url)?||?empty($post_data))?{
??return?false;
?}
?$curlPost?=?$post_data;
?$ch?=?curl_init($url);
?curl_setopt($ch,?CURLOPT_HEADER,?0);
?curl_setopt($ch,?CURLOPT_RETURNTRANSFER,?1);
?if($dataType=='json'){
??curl_setopt($ch,?CURLOPT_HTTPHEADER,?array(
????'Content-Type:?application/x-www-form-urlencoded;charset=UTF-8',
????'Content-Length:?'?.?strlen($curlPost)
???)
??);
?}
?curl_setopt($ch,?CURLOPT_POST,?1);
?curl_setopt($ch,?CURLOPT_POSTFIELDS,?$curlPost);
?curl_setopt($ch,?CURLOPT_SSL_VERIFYPEER,?FALSE);
?curl_setopt($ch,?CURLOPT_SSL_VERIFYHOST,?FALSE);
?$data?=?curl_exec($ch);
?return?$data;
}
//?調(diào)用代碼
function?commit_send($comment_id)??{??
????$comment?=?get_comment($comment_id);?
????$url?=?'企業(yè)微信機(jī)器人webHook地址';
????//?評(píng)論內(nèi)容
?$cont?=?$comment->comment_content;?
?//?評(píng)論人昵稱(chēng)
?$title?=?$comment->comment_author;
?
?
?//?文本消息
????$data?=?array(
?????"msgtype"=>"text",
????????"text"=>array(
????????????"content"=>$cont,
?????????"mentioned_list"=>array("@all")?//?可以@群全部成員
????????)
????);
????
????//?圖文消息(二選一)
?$data?=?array(
??"msgtype"=>"news",
??"news"=>array(
??????"articles"=>array(
??????????array(
??????????????"title"=>date("Y-m-d?H:i:s",time())."@".$title."給你發(fā)來(lái)一條評(píng)論",
????????????????????"description"=>$cont,
????????????????????"url"=>"https://cuixinxin.cn/about",?//?跳轉(zhuǎn)地址
????????????????????"picurl"=>"http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx",?//?圖文消息封面
????????????????)
??????)
????????)
?);
?$res?=?request_post($url,?json_encode($data,'320'),'json');
}
add_action('comment_post',?'commit_send',?19,?2);
server醬版本:
function?commit_send($comment_id)??{??
????//??server醬
????$comment?=?get_comment($comment_id);??
????$text?=?'@'.$comment->comment_author.'發(fā)來(lái)了一條評(píng)論';??
????$desp?=?$comment->comment_content;??
????$key?=?server醬的key;??
????$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('https://sctapi.ftqq.com/'.$key.'.send',?false,?$context);??
}??
add_action('comment_post',?'commit_send',?19,?2);
實(shí)現(xiàn)效果
實(shí)現(xiàn)效果企業(yè)微信注冊(cè)方式
20210208150738.png
Server醬·Turbo版.png我是賣(mài)堅(jiān)果的怪叔叔—— 一枚佛系前端開(kāi)發(fā),會(huì)一丟丟攝影,喜歡折騰,愛(ài)好美食。分享點(diǎn)前端技巧、筆記以及各種有趣的APP和資源教程??
前端公眾號(hào)和交流群

評(píng)論
圖片
表情
