When WordPress receives a comment, it can send a comment notification via email, but the email notification may not be as timely to check, so add a WeChat notification.
Implementation method:
1, the first is through the enterprise WeChat group chat robot to notify.
2. The second way is to implement notifications through the Server sauce
Both approaches have their pros and cons, and there are many ways to notify through the Server Sauce and many options. But after all, they are using someone else’s service, which involves security, stability and membership. Notifications using your own enterprise WeChat do not require the service to be unavailable, and there is no limit to the frequency of notifications. The disadvantage is that there is only one choice.
Code:
Enterprise WeChat version:
date_default_timezone_set("Asia/Shanghai"); Function request_post($url = ", $url = ", $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; Function commit_send($comment_id) {$comment_id = get_comment($comment_id); $url = 'Enterprise WeChat robot webHook address '; $cont = $comment_content; $cont = $comment_content; $comment_comment_author = $comment_comment_author; / / text message $data = array (" msgtype "= >" text ", "text" = > array (" content "= > $cont, "Mentioned_list "=>array("@all") // @all)); $data = array(" msgType "=>"news"); $data = array(" msgType "=>"news"); "News" = > array (" articles "= > array (array (" title =" "> date (" Y -m - d H: I: s", time ()). The "@". $title. "give you sent a comment", "description"=>$cont, "url"=>"https://cuixinxin.cn/about", / / jump address "picurl" = > "http://blogimg.lieme.cn/FmgJkCHkCJWHQdLXAXWjGL204IDx", / / graphic cover)))); $res = request_post($url, json_encode($data,'320'),'json'); } add_action('comment_post', 'commit_send', 19, 2);
Server sauce version:
Function commit_send($comment_id) {$comment_id = $comment_id; $text = '@'.$comment_author.' "; $text = '@'. $desp = $comment->comment_content; $key = key of server; $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);
Implementation effect
Enterprise WeChat registration method
I am a nut selling strange uncle – a Buddha front-end development, will be a lost lost photography, like to toss, love food. Share tips, notes and a variety of interesting APP and resource tutorials