Sunday, November 2, 2025

3 min read

How to Create an AI Text Generator Plugin for WordPress

Let’s make something fun — a lightweight WordPress plugin that talks to OpenAI’s API and generates text based on whatever prompt you enter. Using the latest Chat Completions endpoint, you can build this with just a few lines of PHP.

Step 1: Create Your Plugin Folder

Inside `wp-content/plugins/`, create a new folder called `ai-text-generator`. Inside it, add two files:

  • `ai-text-generator.php` – the main plugin file
  • `admin-page.php` – handles the admin interface

Step 2: Register the Plugin

Open `ai-text-generator.php` and add this code to define and load your plugin:

php
1<?php
2  /**
3   * Plugin Name: AI Text Generator
4   * Description: Generates text using the OpenAI API.
5   */
6  
7  if ( ! defined( 'ABSPATH' ) ) exit;
8  
9  require_once plugin_dir_path( __FILE__ ) . 'admin-page.php';
10  
11  function load_ai_text_generator() {
12      new AI_Text_Generator_Admin_Page();
13  }
14  add_action( 'plugins_loaded', 'load_ai_text_generator' );

Step 3: Create the Admin Page

Now, open `admin-page.php`. This file adds a simple dashboard page where you can enter a prompt and get AI-generated text back.

php
1<?php
2  class AI_Text_Generator_Admin_Page {
3  
4      public function __construct() {
5          add_action( 'admin_menu', array( $this, 'create_menu' ) );
6      }
7  
8      public function create_menu() {
9          add_menu_page(
10              'AI Text Generator',
11              'AI Text Generator',
12              'manage_options',
13              'ai-text-generator',
14              array( $this, 'display_page' ),
15              'dashicons-admin-generic',
16              60
17          );
18      }
19  
20      public function display_page() {
21          ?>
22          <div class="wrap">
23              <h1>AI Text Generator</h1>
24              <form method="POST">
25                  <label for="prompt">Enter a prompt:</label><br>
26                  <input type="text" id="prompt" name="prompt" style="width:100%;max-width:500px;" required>
27                  <p><button type="submit" name="submit" class="button button-primary">Generate Text</button></p>
28              </form>
29          </div>
30          <?php
31          if ( isset( $_POST['submit'] ) && ! empty( $_POST['prompt'] ) ) {
32              $this->generate_text( sanitize_text_field( $_POST['prompt'] ) );
33          }
34      }
35  
36      private function generate_text( $prompt ) {
37          // We'll call OpenAI here.
38      }
39  }

Step 4: Call the OpenAI Chat Completions API

Replace the comment inside `generate_text()` with the following code. This uses the **Chat Completions** endpoint — the latest and preferred way to generate text programmatically.

php
1$api_key = 'YOUR_OPENAI_API_KEY';
2  $ch = curl_init();
3  
4  curl_setopt( $ch, CURLOPT_URL, 'https://api.openai.com/v1/chat/completions' );
5  curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
6  curl_setopt( $ch, CURLOPT_POST, true );
7  curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
8      'Content-Type: application/json',
9      'Authorization: Bearer ' . $api_key,
10  ) );
11  
12  $data = array(
13      'model' => 'gpt-4o-mini', // or gpt-4o, gpt-3.5-turbo
14      'messages' => array(
15          array( 'role' => 'system', 'content' => 'You are a helpful text generator for WordPress users.' ),
16          array( 'role' => 'user', 'content' => $prompt ),
17      ),
18      'max_tokens' => 100,
19  );
20  
21  curl_setopt( $ch, CURLOPT_POSTFIELDS, json_encode( $data ) );
22  
23  $response = curl_exec( $ch );
24  curl_close( $ch );
25  
26  if ( $response ) {
27      $data = json_decode( $response, true );
28      if ( isset( $data['choices'][0]['message']['content'] ) ) {
29          echo '<div class="notice notice-success"><p><strong>AI Response:</strong><br>' . esc_html( trim( $data['choices'][0]['message']['content'] ) ) . '</p></div>';
30      } else {
31          echo '<div class="notice notice-error"><p>Something went wrong. Please try again.</p></div>';
32      }
33  }

This sends your prompt to the `/v1/chat/completions` endpoint, receives the model’s message, and prints it right inside the dashboard. You can swap models or tweak `max_tokens` as needed.

OpenAI API Reference – Chat Completions:

OpenAI Models Overview:

That’s your AI text generator done — built on the newest API structure. From here, you could easily add features like saving results as drafts, logging outputs, or letting users pick a model from a dropdown. Start simple, then keep iterating.

About the Author
Jonas Lindemann

I’m an experienced SEO professional with over a decade of helping over 100 businesses rank higher online, especially local businesses, e-commerce stores and SaaS. As the co-founder of LPagery, I specialize in practical, proven strategies for regular SEO and Local SEO success.