如何用 deepseek 写一个好用的自动发布文章插件

下面是用 deepseek 写好的插件可以直接用

# 操作流程

1. 登录WordPress后台 → 插件 → 安装插件 → 上传插件 → 激活

2. 左侧菜单进入【悟空AI设置】完成API配置

3. 在【采集控制台】设置参数并启动

---

# 插件实现代码 (wp-content/plugins/wukong-ai/wukong-ai.php)

```php

<?php

/*

Plugin Name: 悟空AI自动采集

Description: 多平台AI内容自动生成与发布系统

Version: 1.0

Author: DeepSeek

*/

class Wukong_AI_Plugin {

private $api_base = 'api.deepseek.com/v1/cha';

private $settings;

public function __construct() {

add_action('admin_menu', [$this, 'admin_menu']);

add_action('admin_init', [$this, 'register_settings']);

add_action('wp_ajax_wukong_start_task', [$this, 'start_task']);

$this->settings = get_option('wukong_settings');

}

// 管理菜单

public function admin_menu() {

add_menu_page(

'悟空AI设置',

'悟空AI',

'manage_options',

'wukong-ai',

[$this, 'settings_page'],

'dashicons-edit'

);

}

// 注册设置项

public function register_settings() {

register_setting('wukong_options', 'wukong_settings', [

'sanitize_callback' => [$this, 'sanitize_options']

]);

add_settings_section('main_section', 'API配置', null, 'wukong-ai');

add_settings_field('api_key', 'API密钥', [$this, 'api_key_field'], 'wukong-ai', 'main_section');

add_settings_field('daily_limit', '每日上限(篇)', [$this, 'number_field'], 'wukong-ai', 'main_section');

add_settings_field('target_category', '发布分组', [$this, 'category_select'], 'wukong-ai', 'main_section');

}

// API密钥输入域

public function api_key_field() {

$key = isset($this->settings['api_key']) ? $this->settings['api_key'] : '';

echo '<input type="password" name="wukong_settings[api_key]" value="'.$key.'" class="regular-text">';

}

// 任务控制台界面

public function settings_page() {

?>

<div class="wrap">

<h1>悟空AI内容工厂</h1>

<!-- 统计数据 -->

<div class="card" style="max-width:300px">

<h3>运行状态</h3>

<p>今日已生成:<span id="counter">0</span> 篇</p>

<button id="startTask" class="button-primary">立即生成</button>

<button id="pauseTask" class="button-secondary">暂停</button>

</div>

<!-- 配置表单 -->

<form method="post" action="options.php">

<?php

settings_fields('wukong_options');

do_settings_sections('wukong-ai');

submit_button();

?>

</form>

</div>

<script>

jQuery(document).ready(function($) {

let isRunning = false;

$('#startTask').click(function() {

if(!isRunning) {

isRunning = true;

$(this).text('运行中...');

generateContent();

}

});

function generateContent() {

$.post(ajaxurl, {

action: 'wukong_start_task',

_wpnonce: '<?php echo wp_create_nonce("wukong_nonce"); ?>'

}, function(res) {

$('#counter').text(parseInt($('#counter').text())+1);

if(isRunning) generateContent();

});

}

});

</script>

<?php

}

// 核心生成逻辑

public function start_task() {

check_ajax_referer('wukong_nonce');

// 调用DeepSeek生成内容

$response = wp_remote_post($this->api_base, [

'headers' => [

'Authorization' => 'Bearer ' . $this->settings['api_key'],

'Content-Type' => 'application/json'

],

'body' => json_encode([

'model' => 'deepseek-chat',

'messages' => [

[

'role' => 'user',

'content' => "生成包含以下要素的复合型内容:\n"

."1. 微信公众号格式(包含#标签)\n"

."2. 小红书风格(表情符号和 标签)\n"

."3. 微博体(140字以内)\n"

."主题关于最新AI工具测评"

]

]

])

]);

// 解析响应并发布

if(!is_wp_error($response)) {

$body = json_decode($response['body'], true);

$content = $body['choices'][0]['message']['content'];

// 创建文章

wp_insert_post([

'post_title' => 'AI工具日报 '.date('Y/m/d'),

'post_content' => $content,

'post_status' => 'publish',

'post_category' => [$this->settings['target_category']]

]);

}

wp_die();

}

}

new Wukong_AI_Plugin();



标签: DeepSeek

添加新评论 »