DEV Community

Naimur Rahman Nahid
Naimur Rahman Nahid

Posted on

Integrating OpenAI API with WordPress: Generate Content Automatically

AI is revolutionizing the way we create content — and as WordPress developers, we can bring that power directly into our websites. In this tutorial, I’ll show you how to integrate the OpenAI API with WordPress to automatically generate blog post content, product descriptions, or any text-based data you need.

Why Integrate OpenAI with WordPress?

By connecting OpenAI’s API with WordPress, you can:

Generate SEO-friendly blog posts automatically

Create AI-written summaries or product descriptions

Add chat-based features or auto-reply functionality

Reduce content writing time by 70–80%

This kind of automation can be a real productivity booster for bloggers, agencies, and eCommerce store owners.

Step 1: Get Your OpenAI API Key
Go to OpenAI API Keys
Create a new key and copy it
Keep it safe — you’ll need it in your WordPress code

Step 2: Create a Simple WordPress Plugin
Let’s create a minimal plugin to connect with OpenAI.
Folder structure:

/wp-content/plugins/wp-openai-generator/
- wp-openai-generator.php

wp-openai-generator.php

<?php
/**
 * Plugin Name: WP OpenAI Generator
 * Description: Generate blog content using OpenAI API.
 * Version: 1.0
 * Author: Naimur Rahman Nahid
 */

if ( ! defined( 'ABSPATH' ) ) exit;

// Main function
function nahid_generate_ai_content( $prompt ) {
    $api_key = 'YOUR_OPENAI_API_KEY';
    $endpoint = 'https://api.openai.com/v1/chat/completions';

    $response = wp_remote_post( $endpoint, array(
        'headers' => array(
            'Authorization' => 'Bearer ' . $api_key,
            'Content-Type'  => 'application/json',
        ),
        'body' => json_encode(array(
            'model' => 'gpt-3.5-turbo',
            'messages' => array(
                array('role' => 'user', 'content' => $prompt),
            ),
            'max_tokens' => 300,
        )),
    ));

    if ( is_wp_error( $response ) ) {
        return 'Error: ' . $response->get_error_message();
    }

    $body = json_decode( wp_remote_retrieve_body( $response ), true );
    return $body['choices'][0]['message']['content'] ?? 'No response.';
}

Enter fullscreen mode Exit fullscreen mode

Step 3: Add a WordPress Admin Interface

Let’s make it user-friendly by adding a small settings page in the dashboard.

add_action('admin_menu', function() {
    add_menu_page('AI Content Generator', 'AI Generator', 'manage_options', 'ai-generator', 'nahid_ai_admin_page');
});

function nahid_ai_admin_page() {
    if ( isset($_POST['ai_prompt']) ) {
        $prompt = sanitize_text_field($_POST['ai_prompt']);
        $output = nahid_generate_ai_content($prompt);
        echo '<h3>AI Generated Content:</h3><div style="background:#fff;padding:10px;border:1px solid #ddd;">' . esc_html($output) . '</div>';
    }

    echo '<form method="post">
        <textarea name="ai_prompt" rows="5" cols="60" placeholder="Enter your content idea..."></textarea><br><br>
        <input type="submit" class="button button-primary" value="Generate Content">
    </form>';
}

Enter fullscreen mode Exit fullscreen mode

Now go to:
Dashboard → AI Generator
Enter any topic (like “Write a blog post about WordPress security best practices”) — and OpenAI will generate content for you inside WordPress.

Step 4: Secure Your API Key

Never hardcode your API key in production.
Instead, save it in wp-config.php like this:

define('OPENAI_API_KEY', 'your-secret-key');

Enter fullscreen mode Exit fullscreen mode

And then use:

$api_key = OPENAI_API_KEY;

Enter fullscreen mode Exit fullscreen mode

Step 5: Extend It Further

You can extend this concept by:

Auto-generating post drafts

Adding AI-powered title & meta description generators

Using the WordPress REST API to expose your AI endpoint

Building a Gutenberg block that fetches AI-generated content

AI is not here to replace creators — it’s here to amplify creativity and productivity.
By integrating OpenAI into WordPress, we can automate repetitive content work, enhance SEO, and help users focus on strategy and design instead of typing endlessly.

Top comments (0)