【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个字)、文章链接。测试效果如下:

相关推荐
曹勖之3 小时前
在MATLAB中使用自定义的ROS2消息
开发语言·matlab·机器人·ros·simulink·ros2
陕西艾瑞科惯性技术有限公司4 小时前
让飞行姿态 “可感知”:为什么无人机需要三轴陀螺仪?
嵌入式硬件·机器学习·机器人·无人机·pcb工艺
Bruce_Liuxiaowei5 小时前
PHP文件读取漏洞全面剖析:触发点与利用技术
开发语言·php
hopetomorrow6 小时前
学习路之PHP--webman安装及使用
android·学习·php
wodrpress资源分享6 小时前
wordpress免费主题网站
wordpress
J_Xiong01177 小时前
【VLAs篇】02:Impromptu VLA—用于驱动视觉-语言-动作模型的开放权重和开放数据
语言模型·机器人
链上Sniper21 小时前
Python 区块链开发实战:从零到一构建智能合约
开发语言·网络·python·架构·区块链·php·智能合约
电鱼智能的电小鱼1 天前
无人机巡检智能边缘计算终端技术方案‌‌——基于EFISH-SCB-RK3588工控机/SAIL-RK3588核心板的国产化替代方案‌
网络·人工智能·嵌入式硬件·算法·机器人·无人机·边缘计算
视觉语言导航1 天前
HRI-2025 | 大模型驱动的个性化可解释机器人人机交互研究
人工智能·深度学习·机器人·人机交互·具身智能
链上Sniper1 天前
智能合约安全漏洞解析:从 Reentrancy 到 Integer Overflow
开发语言·网络·架构·区块链·php·智能合约