WordPress AI插件如何实现自动生成文章功能
- Linkreate AI插件 文章
- 2025-08-25 18:49:11
- 7阅读
在WordPress平台上实现AI自动生成文章功能,你需要了解当前市场上几种主流的AI写作插件及其工作原理。这些插件大多基于OpenAI的GPT模型、国产的DeepSeek、豆包或通义千问等大语言模型,通过API调用实现内容自动生成。
主流WordPress AI写作插件对比
目前市面上效果较好的WordPress AI写作插件主要有以下几款:
插件名称 | 支持的AI模型 | 主要特点 | 适用场景 |
---|---|---|---|
AI Writer | OpenAI、DeepSeek | 支持长文生成、SEO优化 | 专业博客、内容网站 |
ContentBot | GPT-4、Gemini、豆包 | 多语言支持、图片生成 | 跨境电商、多语种网站 |
AutoBlog | 通义千问、智普清言 | 批量生成、自动配图 | 资讯站、内容农场 |
安装配置AI写作插件
以AI Writer插件为例,我们来详细讲解安装配置过程。首先,你需要从WordPress后台安装插件:
1. 登录WordPress后台
2. 点击"插件" > "安装插件"
3. 搜索"AI Writer"
4. 点击"现在安装"然后"启用插件"
安装完成后,需要进行API配置。大多数AI写作插件都需要你提供AI服务的API密钥:
// 在WordPress主题的functions.php文件中添加以下代码
function ai_writer_api_config() {
$api_keys = array(
'openai' => 'sk-your-openai-api-key',
'deepseek' => 'your-deepseek-api-key',
'douban' => 'your-douban-api-key'
);
update_option('ai_writer_api_keys', $api_keys);
}
add_action('admin_init', 'ai_writer_api_config');
这段代码会将你的API密钥保存到WordPress数据库中。请注意,API密钥属于敏感信息,建议使用环境变量存储,而不是直接写在代码中。
警告:API密钥泄露可能导致他人滥用你的账户,产生高额费用。在生产环境中,建议使用WordPress的安全插件或服务器环境变量来保护这些敏感信息。
创建AI生成文章的自动化流程
配置完成后,你可以设置自动生成文章的流程。以下是一个基于DeepSeek模型自动生成文章的示例代码:
function generate_article_with_deepseek($keyword, $word_count = 1000) {
$api_url = 'https://api.deepseek.com/v1/chat/completions';
$api_key = get_option('ai_writer_api_keys')['deepseek'];
$prompt = "请为关键词'$keyword'撰写一篇{$word_count}字左右的SEO优化文章,要求包含引言、正文和结论,语言流畅自然。";
$payload = array(
'model' => 'deepseek-chat',
'messages' => array(
array('role' => 'user', 'content' => $prompt)
),
'max_tokens' => $word_count 2,
'temperature' => 0.7
);
$response = wp_remote_post($api_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
),
'body' => json_encode($payload),
'timeout' => 30
));
if (is_wp_error($response)) {
return '生成文章时出错: ' . $response->get_error_message();
}
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['choices'][0]['message']['content'])) {
return $body['choices'][0]['message']['content'];
}
return '无法生成文章,请检查API密钥和参数设置。';
}
这段代码通过DeepSeek的API接口,根据提供的关键词生成指定字数的文章。你可以将此函数与WordPress的定时任务结合,实现定时自动发布文章。
实现自动配图功能
一篇完整的文章不仅需要文字内容,还需要配图。以下代码展示如何使用AI生成图片并自动插入到文章中:
function generate_and_insert_image($post_id, $keyword) {
// 使用DALL-E或Midjourney API生成图片
$image_url = generate_ai_image($keyword);
if ($image_url) {
// 下载图片到WordPress媒体库
$image_id = media_sideload_image($image_url, $post_id, $keyword . '-featured-image');
if (!is_wp_error($image_id)) {
// 设置为特色图片
set_post_thumbnail($post_id, $image_id);
// 在文章内容开头插入图片
$post = get_post($post_id);
$content = $post->post_content;
$image_html = wp_get_attachment_image($image_id, 'large');
$updated_content = $image_html . $content;
wp_update_post(array(
'ID' => $post_id,
'post_content' => $updated_content
));
}
}
}
function generate_ai_image($keyword) {
$api_key = get_option('ai_writer_api_keys')['openai'];
$api_url = 'https://api.openai.com/v1/images/generations';
$payload = array(
'model' => 'dall-e-3',
'prompt' => "为文章'$keyword'生成一张高质量、专业的配图,风格现代简约",
'n' => 1,
'size' => '1024x1024'
);
$response = wp_remote_post($api_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
),
'body' => json_encode($payload),
'timeout' => 30
));
if (!is_wp_error($response)) {
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['data'][0]['url'])) {
return $body['data'][0]['url'];
}
}
return false;
}
这段代码首先调用AI图像生成API创建与文章主题相关的图片,然后将图片下载到WordPress媒体库,设置为文章的特色图片,并插入到文章内容开头。
优化AI生成文章的SEO表现
AI生成的文章虽然内容丰富,但SEO优化仍需额外处理。以下代码展示了如何自动为AI生成的文章添加SEO元素:
function optimize_ai_content_seo($post_id) {
$post = get_post($post_id);
$content = $post->post_content;
$title = $post->post_title;
// 生成SEO描述
$seo_description = generate_seo_description($content);
// 生成标签
$tags = generate_tags_from_content($content);
// 更新文章元数据
update_post_meta($post_id, '_yoast_wpseo_metadesc', $seo_description);
update_post_meta($post_id, '_yoast_wpseo_title', $title . ' - ' . get_bloginfo('name'));
// 添加标签
wp_set_post_tags($post_id, $tags);
}
function generate_seo_description($content) {
// 使用AI生成SEO描述
$api_key = get_option('ai_writer_api_keys')['openai'];
$api_url = 'https://api.openai.com/v1/chat/completions';
$payload = array(
'model' => 'gpt-4',
'messages' => array(
array('role' => 'user', 'content' => "请为以下内容生成一个150字以内的SEO描述:n" . substr($content, 0, 1000))
),
'max_tokens' => 200,
'temperature' => 0.5
);
$response = wp_remote_post($api_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
),
'body' => json_encode($payload),
'timeout' => 30
));
if (!is_wp_error($response)) {
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['choices'][0]['message']['content'])) {
return trim($body['choices'][0]['message']['content']);
}
}
return substr(strip_tags($content), 0, 150);
}
function generate_tags_from_content($content) {
// 使用AI提取关键词作为标签
$api_key = get_option('ai_writer_api_keys')['openai'];
$api_url = 'https://api.openai.com/v1/chat/completions';
$payload = array(
'model' => 'gpt-4',
'messages' => array(
array('role' => 'user', 'content' => "请从以下内容中提取5-8个关键词作为标签,用逗号分隔:n" . substr($content, 0, 1000))
),
'max_tokens' => 100,
'temperature' => 0.3
);
$response = wp_remote_post($api_url, array(
'headers' => array(
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $api_key
),
'body' => json_encode($payload),
'timeout' => 30
));
if (!is_wp_error($response)) {
$body = json_decode(wp_remote_retrieve_body($response), true);
if (isset($body['choices'][0]['message']['content'])) {
$tags_string = trim($body['choices'][0]['message']['content']);
return array_map('trim', explode(',', $tags_string));
}
}
return array();
}
这段代码为AI生成的文章自动添加SEO描述、标题和标签,提高文章在搜索引擎中的排名。你可以将这些函数与文章发布钩子结合,在每次发布AI生成的文章时自动执行SEO优化。
实现定时自动发布功能
要让WordPress网站实现全自动的内容生成和发布,你需要设置定时任务。以下代码展示了如何使用WP-Cron实现定时自动发布AI生成的文章:
// 添加定时任务
function schedule_ai_content_generation() {
if (!wp_next_scheduled('generate_ai_content_event')) {
wp_schedule_event(time(), 'daily', 'generate_ai_content_event');
}
}
add_action('wp', 'schedule_ai_content_generation');
// 定时任务执行函数
function generate_ai_content() {
// 获取关键词列表
$keywords = get_option('ai_writer_keywords', array());
if (empty($keywords)) {
return;
}
// 随机选择一个关键词
$keyword = $keywords[array_rand($keywords)];
// 生成文章内容
$content = generate_article_with_deepseek($keyword, 1500);
// 创建文章
$post_id = wp_insert_post(array(
'post_title' => $keyword,
'post_content' => $content,
'post_status' => 'draft',
'post_author' => 1,
'post_category' => array(1) // 默认分类
));
if (!is_wp_error($post_id)) {
// 生成并插入图片
generate_and_insert_image($post_id, $keyword);
// 优化SEO
optimize_ai_content_seo($post_id);
// 立即发布文章
wp_publish_post($post_id);
}
}
add_action('generate_ai_content_event', 'generate_ai_content');
这段代码设置了一个每天执行一次的定时任务,从预设的关键词列表中随机选择一个关键词,生成文章内容,创建草稿,添加图片,优化SEO,最后发布文章。
配置AI生成的参数和规则
为了提高AI生成文章的质量,你需要配置合适的参数和规则。以下是一个配置界面示例,让你可以在WordPress后台调整这些参数:
// 添加设置菜单
function ai_writer_settings_menu() {
add_options_page(
'AI写作设置',
'AI写作设置',
'manage_options',
'ai_writer_settings',
'ai_writer_settings_page'
);
}
add_action('admin_menu', 'ai_writer_settings_menu');
// 设置页面内容
function ai_writer_settings_page() {
?>
AI写作设置
' . implode("n", $keywords) . '';
echo '
每行一个关键词,系统将随机选择关键词生成文章
';
}
function ai_writer_word_count_callback() {
$word_count = get_option('ai_writer_word_count', 1000);
echo '';
echo '