← Blog

Claude API WordPress Plugin for Content Generation

2026-11-11 · 5 min read · SubToAPI Team

Does a Claude API plugin for WordPress exist?

There's no official Anthropic plugin for WordPress, and most "AI content" plugins in the repository wrap OpenAI models by default, with Claude support either missing or bolted on as an afterthought. If you want Claude specifically — for its longer context, writing style, or your existing workflow — the practical path is a small custom plugin that calls the API directly from your WordPress admin.

That's less work than it sounds. WordPress already gives you wp_remote_post() for HTTP requests, an admin-ajax hook for triggering actions from the post editor, and a settings page API for storing your key. This article walks through building a minimal but functional plugin that generates post drafts, excerpts, and meta descriptions using Claude, and where to plug in an API gateway like SubToAPI so you're not managing raw Anthropic credentials inside your WordPress install.

What the plugin needs to do

A content-generation plugin for WordPress has four jobs:

  1. Store an API key securely (never hardcoded, never in a public option).
  2. Send a prompt built from post title, outline, or existing content.
  3. Handle the response and errors without crashing the editor.
  4. Insert the result into the post content, excerpt, or a custom field.

Step 1: Settings page for the API key

add_action('admin_menu', function () {
    add_options_page('Claude Content Generator', 'Claude Generator', 'manage_options', 'claude-generator', 'claude_generator_settings_page');
});

add_action('admin_init', function () {
    register_setting('claude_generator', 'claude_generator_api_key');
});

function claude_generator_settings_page() {
    ?>
    <div class="wrap">
        <h1>Claude Content Generator Settings</h1>
        <form method="post" action="options.php">
            <?php settings_fields('claude_generator'); ?>
            <table class="form-table">
                <tr>
                    <th>API Key</th>
                    <td><input type="password" name="claude_generator_api_key"
                        value="<?php echo esc_attr(get_option('claude_generator_api_key')); ?>"
                        class="regular-text"></td>
                </tr>
            </table>
            <?php submit_button(); ?>
        </form>
    </div>
    <?php
}

Storing a single key here is simple, but it ties the key's spend limits and rotation to whatever you set at the provider level. Using a SubToAPI key (sub_live_...) instead of a raw Anthropic key means you can rotate it, cap usage per site, and see token consumption in one dashboard without touching this code again — useful if you run content generation across several WordPress installs.

Step 2: The generation request

function claude_generator_call_api($prompt) {
    $api_key = get_option('claude_generator_api_key');

    $response = wp_remote_post('https://api.subtoapi.app/v1/messages', [
        'headers' => [
            'Authorization' => 'Bearer ' . $api_key,
            'Content-Type'  => 'application/json',
        ],
        'body' => wp_json_encode([
            'model'      => 'claude-sonnet-4-20250514',
            'max_tokens' => 800,
            'messages'   => [
                ['role' => 'user', 'content' => $prompt],
            ],
        ]),
        'timeout' => 60,
    ]);

    if (is_wp_error($response)) {
        return new WP_Error('claude_api_error', $response->get_error_message());
    }

    $body = json_decode(wp_remote_retrieve_body($response), true);
    return $body['content'][0]['text'] ?? '';
}

Test the same request with curl before wiring it into PHP, so you can isolate network issues from plugin bugs:

curl https://api.subtoapi.app/v1/messages \
  -H "Authorization: Bearer $SUBTOAPI_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "model": "claude-sonnet-4-20250514",
    "max_tokens": 800,
    "messages": [{"role": "user", "content": "Write a 150-word meta description for a post about composting in small apartments."}]
  }'

Full request and response fields are documented at /docs/messages, and /docs/quickstart covers getting a key and your first call end to end.

Step 3: Admin-ajax hook for the editor button

add_action('wp_ajax_claude_generate_excerpt', function () {
    check_ajax_referer('claude_generator_nonce');
    $post_id = intval($_POST['post_id']);
    $content = get_post_field('post_content', $post_id);

    $prompt = "Write a concise, engaging excerpt (max 40 words) for this blog post:\n\n" . wp_strip_all_tags($content);
    $result = claude_generator_call_api($prompt);

    if (is_wp_error($result)) {
        wp_send_json_error($result->get_error_message());
    }

    wp_send_json_success(['excerpt' => $result]);
});

On the JavaScript side, a small script in the block editor sidebar sends the post ID, shows a loading state, and fills the excerpt field with the response. This is also where streaming becomes relevant if you're generating longer drafts — for a 1,500-word post you don't want the admin screen to sit frozen for 15 seconds waiting on a single response. SubToAPI supports streaming for exactly this case; see /docs/streaming for the server-sent events format if you want to show the draft appearing incrementally.

Handling rate limits and failures gracefully

Content generation inside a CMS admin screen fails in visible, annoying ways if you don't plan for it: editors sitting on a spinning button, lost drafts, duplicate requests from double-clicks. A few practical rules:

If multiple editors on your team use the plugin, a shared SubToAPI key with per-seat visibility (available from the Team plan) lets you see which site or user is driving usage, rather than one opaque Anthropic bill. Check /pricing for the Solo, Team, and Scale tiers, and /signup includes a free trial if you want to test the integration before committing.

Beyond excerpts: outline-to-draft generation

The same pattern extends to full draft generation. Instead of an excerpt prompt, send the post title and a bullet outline, ask for a structured draft with headings, and insert the result into the block editor using wp.blocks.rawHandler() to convert HTML into blocks automatically. If your prompts need the model to call back into WordPress data — pulling related posts or checking existing tags before writing — that's a case for tool use rather than a single plain-text prompt; /docs/tools covers defining and handling tool calls if you go that route.

questions

Do I need Anthropic API access directly, or can I use a gateway? Either works. A gateway like SubToAPI sits in front of the same models and gives you an HTTPS endpoint, usage metadata, and team key management — useful if multiple sites or editors need controlled access without sharing one raw API key.

Will this slow down the WordPress admin? Generation happens via AJAX in the background, so the editor stays responsive. The only wait is for the API response itself, which is typically a few seconds for excerpts and longer for full drafts — streaming helps here.

Can I generate content for custom post types, not just blog posts? Yes. The AJAX handler works with any post type since it operates on post_id and post_content fields that exist across WordPress's content model, not just the default post type.

Turn your Claude access into an HTTPS API

SubToAPI gives you application API keys, streaming, tool use and usage insights on top of your existing Claude access — set up in minutes.

Start free  Read the quickstart →