【WordPress】发布文章时自动通过机器人推送到钉钉

在您的主题下functions.php中添加如下代码:

bash 复制代码
function wpso_dingding_publish_notify($post_ID) {
    // 获取文章对象
    $post = get_post($post_ID);
    
    // 检查是否是文章首次发布(即不是修订版)
    if (get_post_status($post_ID) == 'publish' && !get_post_meta($post_ID, '_wpso_dingding_sent', true)) {
        // 文章的标题、人工摘要和链接
        $title = get_the_title($post_ID);
        $excerpt = $post->post_excerpt;  // 获取文章的人工设置摘要
        if (empty($excerpt)) {
            // 如果没有设置摘要,使用文章内容的前20个字作为摘要
            $excerpt = wp_trim_words(get_post_field('post_content', $post_ID), 20);
        }
        $url = get_permalink($post_ID);
        
        // 发送的消息内容
        $text = get_bloginfo('name') . ' 上有新的文章发布啦!!';
        $desp = '文章标题:' . $title . "\n" . '文章摘要:' . $excerpt . "\n" . '文章链接:' . $url;

        // 钉钉机器人的 Access Token
        $access_token = '你的Token';
        
        // 构造发送数据
        $data = array(
            'msgtype' => 'text',
            'text' => array(
                'content' => $text . "\n" . $desp
            )
        );
        
        // 将数据编码为JSON格式
        $json_data = json_encode($data, JSON_UNESCAPED_UNICODE);
        
        // 配置HTTP请求
        $opts = array('http' => array(
            'method'  => 'POST',
            'header'  => 'Content-type: application/json',
            'content' => $json_data
        ));
        
        // 创建HTTP上下文
        $context = stream_context_create($opts);
        
        // 发送钉钉推送
        $result = file_get_contents('https://oapi.dingtalk.com/robot/send?access_token=' . $access_token, false, $context);
        
        // 标记文章已发送,避免重复发送
        update_post_meta($post_ID, '_wpso_dingding_sent', true);
    }
}
add_action('publish_post', 'wpso_dingding_publish_notify');

即可实现自动推送,推送内容包括文章标题、文章设置的人工摘要(若无则提取文章内容前20个字)、文章链接。测试效果如下:

相关推荐
H5css�海秀8 小时前
今天是自学大模型的第一天(sanjose)
后端·python·node.js·php
xingxin329 小时前
日志文件分析溯源(连接WebShell的IP地址)实验报告
安全·web安全·网络安全·php·文件上传
LegendNoTitle9 小时前
计算机三级等级考试 网络技术 选择题考点详细梳理
服务器·前端·经验分享·笔记·php
PiKaMouse.10 小时前
navigation2-humble从零带读笔记第一篇:nav2_core
c++·算法·机器人
CoovallyAIHub13 小时前
多 Agent 手术推理框架:Agent 辩论+RAG 补上手术知识,零样本超越监督基线 14.6 个百分点
算法·架构·机器人
2601_9540236616 小时前
Beyond the Hype: Deconstructing the 2025 High-Performance Stack for Agencies
java·开发语言·算法·seo·wordpress·gpl
鲁邦通物联网16 小时前
架构实战:机房轮式巡检机器人梯控的非侵入式边缘解耦设计
机器人·巡检机器人·机器人梯控·机器人乘梯·机器人自主乘梯·agv机器人梯控·巡检机器人梯控
瑞璐塑业peek注塑17 小时前
关节执行器:一体化成型PEEK轴承注塑方案,实现机器人轻量化运行
机器人
三七吃山漆17 小时前
[红明谷CTF 2021]write_shell
php·ctf·[红明谷ctf 2021]
CyanMind18 小时前
IsaacLab 训练范式探索(一):让机器人拥有“记忆”的 RNN 策略
人工智能·rnn·机器人