AI自动写文章插件WordPress API调用方法和工具推荐2025

WordPress AI写作插件开发基础

WordPress作为全球最流行的内容管理系统,与AI技术的结合为内容创作者带来了革命性的变化。开发AI自动写文章插件需要理解WordPress插件架构和AI服务的API调用机制。

WordPress插件开发遵循特定的文件结构和命名规范。一个基础的AI写作插件至少需要一个主PHP文件,通常命名为"ai-writer.php"或类似名称。这个文件必须包含插件头信息,以便WordPress能够识别和管理插件。


/
  Plugin Name: AI Auto Writer
  Plugin URI: https://example.com/ai-auto-writer
  Description: Automatically generate articles using AI services
  Version: 1.0.0
  Author: Your Name
  Author URI: https://example.com
  License: GPL v2 or later
  License URI: https://www.gnu.org/licenses/gpl-2.0.
  Text Domain: ai-auto-writer
 /

// 防止直接访问此文件
if (!defined('ABSPATH')) {
    exit;
}

主流AI服务API集成

当前市场上有多种AI服务可供集成到WordPress插件中,包括OpenAI的ChatGPT、Google的Gemini、百度的文心一言等。每种服务都有其特定的API调用方式和参数设置。

OpenAI ChatGPT API集成

OpenAI的ChatGPT是目前最流行的AI文本生成服务之一。要在WordPress插件中集成ChatGPT API,需要使用API密钥进行身份验证,并通过HTTP请求与API端点交互。


function chatgpt_generate_content($prompt, $api_key) {
    $url = 'https://api.openai.com/v1/chat/completions';
    
    $headers = array(
        'Content-Type: application/json',
        'Authorization: Bearer ' . $api_key
    );
    
    $body = array(
        'model' => 'gpt-4',
        'messages' => array(
            array(
                'role' => 'user',
                'content' => $prompt
            )
        ),
        'max_tokens' => 1500,
        'temperature' => 0.7
    );
    
    $args = array(
        'headers' => $headers,
        'body' => json_encode($body),
        'timeout' => 30
    );
    
    $response = wp_remote_post($url, $args);
    
    if (is_wp_error($response)) {
        return 'Error: ' . $response->get_error_message();
    }
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    
    if (isset($data['choices'][0]['message']['content'])) {
        return $data['choices'][0]['message']['content'];
    } else {
        return 'Error generating content.';
    }
}

Google Gemini API集成

Google的Gemini是另一个强大的AI文本生成服务。与ChatGPT类似,集成Gemini API也需要API密钥和HTTP请求,但请求格式和参数有所不同。


function gemini_generate_content($prompt, $api_key) {
    $url = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-pro:generateContent?key=' . $api_key;
    
    $headers = array(
        'Content-Type: application/json'
    );
    
    $body = array(
        'contents' => array(
            array(
                'parts' => array(
                    array(
                        'text' => $prompt
                    )
                )
            )
        ),
        'generationConfig' => array(
            'temperature' => 0.7,
            'topK' => 40,
            'topP' => 0.95,
            'maxOutputTokens' => 1500,
        )
    );
    
    $args = array(
        'headers' => $headers,
        'body' => json_encode($body),
        'timeout' => 30
    );
    
    $response = wp_remote_post($url, $args);
    
    if (is_wp_error($response)) {
        return 'Error: ' . $response->get_error_message();
    }
    
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    
    if (isset($data['candidates'][0]['content']['parts'][0]['text'])) {
        return $data['candidates'][0]['content']['parts'][0]['text'];
    } else {
        return 'Error generating content.';
    }
}

百度文心一言API集成

百度的文心一言是针对中文内容优化的AI服务,特别适合中文WordPress网站的内容生成。文心一言API的调用方式与上述两种服务有所不同。


function ernie_generate_content($prompt, $api_key, $secret_key) {
// 获取access token
$token_url = 'https://aip.baid