超简单!两步实现Wordpress评论微信通知~

执行上下文

共 5607字,需浏览 12分钟

 · 2021-08-03

戳上方“执行上下文”,选择“置顶公众号

关键时刻,第一时间送达!

WordPress收到评论的时候可以通过邮件发送评论通知,但是邮件通知的可能不是那么能及时的查看,所以增加一个微信通知。

实现方式:

1、第一种就是通过企业微信群聊机器人来通知。
2、第二种就是通过server酱来实现通知

这两种方式各有好坏,通过server酱通知的方式很多,选择性很多。但是毕竟是用的别人的服务,涉及安全,稳定以及需要会员的问题。用自己的企业微信来通知就不需要考虑服务不可用的问题,通知频率也不会限制。缺点是选择性就一个。

代码:

企业微信版本:

date_default_timezone_set("Asia/Shanghai");

// 请求体
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;
}

// 调用代码
function commit_send($comment_id)  {  
    $comment = get_comment($comment_id); 
    $url = '企业微信机器人webHook地址';
    // 评论内容
 $cont = $comment->comment_content; 
 // 评论人昵称
 $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."给你发来一条评论",
                    "description"=>$cont,
                    "url"=>"https://cuixinxin.cn/about", // 跳转地址
                    "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.'发来了一条评论';  
    $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);

实现效果

实现效果

企业微信注册方式

20210208150738.png
Server酱·Turbo版.png

我是卖坚果的怪叔叔—— 一枚佛系前端开发,会一丢丢摄影,喜欢折腾,爱好美食。分享点前端技巧、笔记以及各种有趣的APP和资源教程♥♥

前端公众号和交流群



浏览 57
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报