关于wordpress:超简单两步实现WordPress评论微信通知

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);

实现成果

企业微信注册形式

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

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理