Developer · Guide
How to Connect an AI Model to WordPress (APIs and Plugins)
Short answer
To connect an AI model to WordPress, install a plugin such as AI Engine or the WordPress.org AI plugin, add your provider API key in its settings, and pick a model. For custom work, send a request to the provider API with wp_remote_post() and store the key outside public files.
Key takeaways
- A plugin is the fastest route: install AI Engine, add your API key, pick a model, and send one prompt.
- OpenAI lists GPT-5 at $1.25 input and $10.00 output per 1 million tokens as of November 2026.
- Claude Sonnet 5.5 costs $2 input and $10 output per 1 million tokens as of November 2026.
- Application Passwords, introduced in WordPress 5.6, let outside tools use the REST API over HTTPS.
- Never publish AI output without a human check, because models can invent facts, dates, and links.
- Store API keys in wp-config.php or a host secrets store, never in public theme files or posts.
What does it mean to connect an AI model to WordPress?
Connecting an AI model means your site can send text to a model and use its reply.
An AI model is a service that reads text and writes text back. Your site sends a prompt, such as a draft title or a visitor question, and the model returns words, a summary, or code. The link runs over the web, through an API key.
There are three main routes. A plugin holds the key and adds ready-made features.
Custom code calls the provider API directly. An outside tool calls your site through the WordPress REST API.
What do you need before you start?
You need a WordPress site with HTTPS, an admin account, and an API key from a model provider.
HTTPS matters because keys travel in request headers. Application Passwords from WordPress are available by default only on sites served over HTTPS, according to the WordPress developer handbook. A local test site can use them too.
You also need a clear job for the model. Pick one task first, such as drafting summaries or answering common questions. A single job keeps costs low and makes the result easy to check.
- A WordPress admin account with permission to install plugins
- HTTPS turned on for your site
- An API key from OpenAI, Anthropic, or Google
- Billing set up with that provider, so calls are not refused
- A backup of your site before you add new code or plugins
Which AI model should you pick?
Pick the cheapest model that handles your job well, and start with a small model for simple text tasks.
Model prices are charged per 1 million tokens, and a token is a small piece of text.
OpenAI lists GPT-5 at $1.25 per 1 million input tokens and $10.00 per 1 million output tokens as of November 2026, with GPT-5-nano at $0.05 and $0.40. That gap makes the small model far cheaper for short replies.
Anthropic lists Claude Sonnet 5.5 at $2 per 1 million input tokens and $10 per 1 million output tokens as of November 2026.
Its Claude Haiku 5.5 costs $0.10 input and $0.50 output for prompts up to 100,000 tokens. Use Haiku for high-volume sorting or short answers, and Sonnet for longer drafts.
Google lists Gemini 2.5 Flash-Lite at $0.10 input and $0.40 output per 1 million tokens as of November 2026. Its Gemini 3.8 Flash costs $0.75 input and $3.75 output through 31 December 2026, then $1.50 and $7.50 from 1 January 2027.
| Model | Input per 1M tokens | Output per 1M tokens | Best for |
|---|---|---|---|
| GPT-5-nano (OpenAI) | $0.05 | $0.40 | Short, simple replies |
| GPT-5 (OpenAI) | $1.25 | $10.00 | General writing and chat |
| Claude Haiku 5.5 (Anthropic) | $0.10 | $0.50 | Fast, high-volume tasks |
| Claude Sonnet 5.5 (Anthropic) | $2.00 | $10.00 | Longer drafts and editing |
| Gemini 2.5 Flash-Lite (Google) | $0.10 | $0.40 | Low-cost, high-volume tasks |
| Gemini 3.8 Flash (Google) | $0.75 | $3.75 | Everyday chat and drafts |
Prices above are list prices as of November 2026. OpenAI pricing lists the full GPT table on its own page.
Anthropic pricing lists the full Claude table on its own page.
Gemini pricing lists the full Gemini table on its own page.
How do you connect an AI model with a plugin?
Install a plugin, add your API key in its settings, pick a model, and send one short prompt.
The plugin route takes about 15 to 30 minutes. It needs no code, and the plugin handles the API calls for you. This is the best route for most site owners.
Step 1: Install AI Engine
- Go to “Plugins → Add New” in your WordPress admin.
- Search for “AI Engine” and click “Install Now”, then “Activate”.
- Open the plugin settings and add your provider API key.
- Pick a model, such as GPT-5 or Claude Sonnet 5.5.
- Save the settings and send one short prompt in the plugin playground or chatbot.
AI Engine is listed on WordPress.org with 90,000+ active installations and a 4.9 out of 5 rating as of November 2026.
Its WordPress.org page says it connects WordPress with OpenAI, Anthropic, Google, Mistral, and more, using your own API keys. The free plugin covers chatbots and content tools, while Pro adds paid features.
Step 2: Try the official WordPress AI plugin
The WordPress.org AI plugin adds opt-in AI tools to the admin and Block Editor. Nothing is turned on when you activate it, so you choose each item in “Settings → AI”.
You need a connector plugin and your own API key, added in “Settings → Connectors”, and your provider bills you for usage.
This plugin suits owners who want to try WordPress AI features early. Read our guide to WordPress AI features before you switch features on. The plugin needs the Block Editor, and the Classic Editor is not supported.
Step 3: Add a chatbot or a writing helper
Once the key works, add one visible feature. A chatbot answers visitor questions, while a writing helper drafts titles inside the editor.
Our guide to adding an AI chatbot to WordPress walks through the chatbot setup. Check its answers for a week before you add a second feature.
How do you connect an AI model with code and an API key?
Use WordPress code to send a POST request to the provider API, with your key in the header.
The code route takes about 1 to 2 hours. It gives you full control over prompts, limits, and where replies are saved.
Step 1: Get an API key
- Create an account on the provider platform, such as OpenAI, Anthropic, or Google AI Studio.
- Open the API keys page and create a new secret key.
- Copy the full key at once, because OpenAI shows it only once at creation.
- Add billing to the provider account, so calls are not refused for lack of funds.
- Store the key in a safe place, such as a password manager, until you add it to your site.
Step 2: Store the key on your site
Never put the key in theme files that you share or in a public code repo. Add it to wp-config.php as a constant, or use a secrets plugin from your host. That way the key stays out of your theme and out of version control.
Step 3: Send a request with wp_remote_post()
WordPress has a built-in function for this job. wp_remote_post() performs an HTTP request using the POST method and returns its response, according to the WordPress developer reference. It was introduced in version 2.7.0, and it returns an array or a WP_Error object on failure.
$response = wp_remote_post(
'https://api.openai.com/v1/chat/completions',
array(
'headers' => array(
'Authorization' => 'Bearer ' . OPENAI_API_KEY,
'Content-Type' => 'application/json',
),
'body' => wp_json_encode( array(
'model' => 'gpt-5',
'messages' => array(
array(
'role' => 'user',
'content' => 'Write a one-sentence summary of this post.',
),
),
) ),
'timeout' => 30,
)
);
Check the reply code before you use the body. A 200 code means the call worked, a 401 code means the key is wrong or revoked, and a 429 code means you hit a rate or billing limit. Log errors, and show visitors a plain message instead of the raw error.
How do you connect WordPress to an AI model from outside WordPress?
Use an Application Password so an outside tool can call the WordPress REST API as a user.
This route lets an outside app, script, or AI agent read or write posts on your site. It is the route to use when the AI model lives outside WordPress and needs your content.
Step 1: Create an Application Password
- Go to “Users → Profile” in your WordPress admin.
- Find the “Application Passwords” section near the bottom.
- Type a name, such as “AI writer”, and click “Add New Application Password”.
- Copy the password at once, because WordPress shows it only once.
Application Passwords were introduced in WordPress 5.6 in December 2020. The WordPress handbook says they are tied to one user, stored hashed, and cannot be used to log in at wp-login.php. They work for the REST API and XML-RPC only.
Step 2: Call the REST API with that password
Send the username and the Application Password with Basic Auth over HTTPS. The REST API handbook lists cookie auth for code inside WordPress, and Application Passwords for anything outside it. Use a dedicated user with only the roles the task needs, so a leaked password cannot change more than that task allows.
Step 3: Use the Abilities API on newer WordPress
WordPress 6.9 added the Abilities API to core. An ability is one action a plugin or WordPress itself can perform, with a name, a description, and a permission check. Abilities can be listed and run through the REST API at /wp-json/wp-abilities/v1/, and access needs an authenticated user.
How much does it cost to run an AI model on WordPress?
You pay the model provider per token, plus any paid plugin licence.
AI Engine is free on WordPress.org, and you pay your provider directly for each call. Its Pro licence is paid, so check the vendor page for the current price before you buy. The WordPress.org AI plugin is also free, and your provider bills you for usage.
Token costs add up with long prompts and long replies. Output tokens cost far more than input tokens on most models, so keep replies short.
OpenAI lists GPT-5 output at $10.00 per 1 million tokens against $1.25 for input as of November 2026. Anthropic lists Claude Opus 5.5 at $4 input and $20 output per 1 million tokens as of November 2026.
| Cost item | What you pay | How to keep it low |
|---|---|---|
| Model calls | Per 1M input and output tokens | Use a small model for simple jobs |
| Long replies | Output tokens, often 5 to 8 times input price | Ask for short replies and set a max length |
| Batch jobs | Half the standard rate on OpenAI and Gemini | Send non-urgent jobs in a batch |
| Plugin licence | Free core, paid Pro on some plugins | Start free and upgrade only when you need Pro |
| Wasted calls | Every call, even a failed one that ran | Cache repeated answers and limit calls per visitor |
Set a spending limit in your provider account before you go live. Check the usage page after the first week, and lower the model size or the reply length if the bill is higher than you planned.
How do you check that the connection works?
Send one short prompt, confirm the reply arrives, and confirm the result lands where you planned.
Start with a prompt you can judge at once, such as “Summarise this post in one sentence.” A good reply is short, on topic, and free of invented facts about your post. A bad reply, or no reply, means the key, model name, or billing needs a fix.
- The API returns a 200 code, not 401 or 429.
- The reply text appears in the plugin, editor, or log where you planned it.
- The provider usage page shows 1 call, with the model name you picked.
- A second prompt with different words gives a different, sensible reply.
- No API key appears on any public page or in your site source.
If the reply is wrong, change one thing at a time. First check the key, then the model name, then the prompt. Our guide to using ChatGPT with WordPress covers common setup checks for that provider.
How do you keep your API key safe?
Treat the key like a password, store it outside public files, and revoke it at once if it leaks.
A leaked key lets anyone spend your provider balance. Never paste a key into a post, a comment, a support forum, or a public code repo. Never send it to a visitor through the browser, because browser code is visible to everyone.
- Store the key in
wp-config.phpor a host secrets store, not in theme files. - Give each site and each tool its own key, so you can revoke one without breaking the rest.
- Set a monthly spending limit in the provider account.
- Revoke a key in the provider dashboard as soon as you suspect a leak.
- Use a low-role WordPress user for Application Passwords, not an admin.
Check your provider usage page each week for the first month. A sudden jump in calls often means a key leaked or a loop is calling the model on every page load.
What mistakes should you avoid?
Avoid pasting keys into public places, letting the model publish without review, and picking the dearest model for simple jobs.
The most common mistake is letting AI output go live without a human check. Models can invent facts, dates, and links, so read every draft before you publish it. Our guide to using AI to write blog posts in WordPress stresses the same review step.
- Do not let the model publish posts on its own.
- Do not send private visitor data to a model without a clear need.
- Do not use the dearest model for sorting, tagging, or short replies.
- Do not leave a chatbot with no spending limit or rate limit.
- Do not skip the backup before you add AI code or plugins.
- Do not use the same key on every site you own.
If you plan to use AI output for search traffic, read our guide to using AI for WordPress SEO before you publish at scale.
Frequently asked questions
Do I need to know code to connect an AI model to WordPress?
No. Install a plugin such as AI Engine or the WordPress.org AI plugin, add your API key in its settings, and pick a model. Code is only needed when no plugin does exactly what you want, such as saving replies to a custom field or running a special prompt on a schedule.
Which AI provider is cheapest for a small WordPress site?
Small models are cheapest. As of November 2026, GPT-5-nano and Gemini 2.5 Flash-Lite both list $0.10 or less per 1 million input tokens, while Claude Haiku 5.5 lists $0.10 input. Pick the cheapest model that handles your task well, keep replies short, and check your provider usage page after the first week.
Can I use my ChatGPT subscription instead of an API key?
No, a ChatGPT subscription and the API platform have separate billing. A plugin or custom code needs an API key from the provider platform, with billing set up there. Some plans include API credits, but the key still comes from the API platform, not from the chat app.
Is it safe to put an API key in a WordPress plugin?
It is safe when the plugin stores the key in your database and never shows it on public pages. Store keys in wp-config.php or a host secrets store when you use custom code, give each site its own key, set a spending limit, and revoke a key at once if you think it leaked.
What happens if my API key stops working?
Check three things in order. First, confirm billing is active and the key has not been revoked or expired. Second, confirm the model name is still offered by your provider, because old models are retired. Third, check the error code: 401 means a bad key, 429 means a rate or billing limit, and 500 means a provider fault.
Can an AI model publish posts on my WordPress site by itself?
It can only do that if you give it a way to write, such as an Application Password or an Abilities API tool with write permission. Do not grant that access without a review step. Let the model draft content, read each draft yourself, and publish only after you have checked its facts and links.
Sources
- Application Passwords WordPress Developer Resources
- wp_remote_post() WordPress Developer Resources
- Pricing | OpenAI API OpenAI
- Pricing - Claude Platform Docs Anthropic
- Gemini Developer API pricing Google
- AI Engine - The Chatbot, AI Framework & MCP for WordPress WordPress.org
- AI - WordPress plugin WordPress.org
- Authentication - REST API Handbook WordPress Developer Resources
Is your product missing, or something out of date?
Tell us what to add or correct. We read every note and reply when we need a source. A message does not buy a ranking.
Thanks — we review requests within 3 business days.
