← Blog

Claude API WordPress Plugin Integration Guide

2026-10-22 · 5 min read · SubToAPI Team

How to integrate the Claude API into WordPress

There's no official Anthropic WordPress plugin, so "Claude API WordPress plugin integration" almost always means one of two things: using a general-purpose AI content plugin that lets you plug in a custom API endpoint, or building a small custom plugin that calls Claude directly from PHP. Both work, but they have very different security and maintenance implications.

The fastest, most reliable path for most sites is a custom plugin that talks to an HTTPS API — either Anthropic's Messages API directly, or a gateway like SubToAPI that wraps your existing Claude access in a standard API key (sub_live_...) with usage tracking and team seats. This article walks through the actual PHP code, where to store secrets, and where WordPress's architecture creates friction (mainly around streaming).

Why you need an API layer, not raw credentials

WordPress plugins run server-side PHP, which is good — your API key never touches the browser if you write the integration correctly. But WordPress also has a plugin ecosystem with wildly inconsistent security practices, auto-update behavior, and shared hosting environments where file permissions are loose. That makes key hygiene non-negotiable:

If you're already paying for Claude through a personal or team account, a tool like SubToAPI turns that access into a proper API key with request logging, streaming support, and per-seat billing, which is useful once you have more than one developer touching the integration. Plans start at €9/month on the Solo tier.

Option 1: Existing AI plugins with a custom endpoint field

Several WordPress AI plugins (content generators, chatbot widgets, SEO assistants) let you paste in a custom "OpenAI-compatible" or generic REST endpoint plus an API key. If the plugin supports a configurable base URL and bearer token, you can point it at https://api.subtoapi.app/v1/messages and your key. This gets you a UI fast but gives you less control over prompts, system instructions, and tool use.

Option 2: Build a minimal custom plugin

For anything beyond trivial text generation, write your own plugin. Here's a working skeleton.

1. Store the key safely — add to wp-config.php, above the "stop editing" line:

define( 'SUBTOAPI_KEY', 'sub_live_xxxxxxxxxxxxxxxx' );

2. Register a WordPress REST route that proxies the request:

add_action( 'rest_api_init', function () {
    register_rest_route( 'claude-plugin/v1', '/generate', [
        'methods'             => 'POST',
        'callback'            => 'claude_plugin_generate',
        'permission_callback' => function () {
            return current_user_can( 'edit_posts' );
        },
    ] );
} );

function claude_plugin_generate( WP_REST_Request $request ) {
    $prompt = sanitize_textarea_field( $request->get_param( 'prompt' ) );

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

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

    $body = json_decode( wp_remote_retrieve_body( $response ), true );
    return rest_ensure_response( $body );
}

This keeps the key server-side and gates the endpoint behind a WordPress capability check (edit_posts here, adjust as needed). The permission callback matters — without it, anyone could hit your proxy and burn through your Claude usage.

3. Call it from the admin editor or front end with JavaScript:

const res = await fetch('/wp-json/claude-plugin/v1/generate', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-WP-Nonce': wpApiSettings.nonce,
  },
  body: JSON.stringify({ prompt: 'Write a 2-sentence product summary for a ceramic mug.' }),
});
const data = await res.json();
console.log(data.content[0].text);

Full request/response shapes and parameters are documented at /docs/messages and the general /docs/quickstart if you're setting this up for the first time.

Streaming inside WordPress

Server-Sent Events work fine from a standalone Node server, but admin-ajax.php and most shared hosting PHP-FPM setups buffer output, which breaks token-by-token streaming. If you need a typing-style UI, two practical options:

  1. Run the streaming connection through a small separate Node/Express endpoint (not inside WordPress) and have the WordPress page fetch from there.
  2. Poll a WordPress REST endpoint that calls Claude without streaming and returns the full response, accepting a short delay instead of a live stream.

If you do run streaming from a non-PHP service, the mechanics are covered in /docs/streaming.

Using tool calling for structured WordPress content

Tool use is useful here: instead of parsing freeform text to extract a title, excerpt, and tags from Claude's reply, define a tool schema and get structured JSON back that maps directly to WordPress post fields (post_title, post_excerpt, taxonomy terms). This avoids regex-parsing prose inside your plugin. See /docs/tools for schema examples.

Shortcode for front-end generation

add_shortcode( 'claude_answer', function ( $atts ) {
    $atts = shortcode_atts( [ 'prompt' => '' ], $atts );
    return '<div class="claude-output" data-prompt="' . esc_attr( $atts['prompt'] ) . '"></div>';
} );

Pair this with a small enqueued script that calls your REST proxy on page load and injects the result — useful for FAQ blocks, product blurbs, or support widgets generated on the fly.

Questions

Does Anthropic publish an official WordPress plugin? No. Integration happens through custom code or third-party plugins that accept a configurable API endpoint and key.

Can I call Claude directly from browser JavaScript in a WordPress theme? Don't — it exposes your API key to anyone viewing page source. Always proxy through a WordPress REST route or PHP endpoint.

Why doesn't streaming work well with WordPress? Standard PHP hosting setups (PHP-FPM, admin-ajax.php) buffer output and don't handle Server-Sent Events well. Route streaming through a separate lightweight server instead. If you need a managed layer with logging and seats across a team editing content, /pricing outlines the tiers; sign up at /signup to get a key.

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 →